Devphil Devphil - 6 months ago 96 Question

C# DataGridView capturing deleted row


I am using a DataGridView, which is bound to a dataset.
There is a bindingNavigator as well.

when a user deletes a row by clicking "bindingNavigatorDeleteItem" button, I am trying to get the row being deleted.

private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
int crow = gridEventType.CurrentCell.RowIndex;

but it returns the newly selected row AFTER deletion of a selected row.
so, If I have two rows
and I delete 2

crow is 1, not 2.

Am I missing something here?

2. How do I know whether certain rows are modified in datagridview?


Answer Source

You should be using the DataGridViewCellEventArgs.

private void bindingNavigatorDeleteItem_Click(object sender, 
                                                  DataGridViewCellEventArgs e) 
   int cRow =  gridEventType.Rows[e.RowIndex];

   //delete row from persisted storage

   //rebind data to persisted storage or remove row directly:

For your second point, there's a CellValueChangedEvent.

    private void bindingNavigatorModifiedCell_CellValueChanged(object sender, 
                                                   DataGridViewCellEventArgs e)
         //updated row
         int cRow = e.RowIndex;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download