Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

DataGridViewRow.DataBoundItem throws IndexOutOfRangeException

$
0
0
Hi,
I have a DataGridView bound to a dataset and am trying to validate a row during the RowValidating event.  I want to access the DataRow being validated and get its RowState, however sometimes when I call the property
DataBoundItem I get an IndexOutOfRangeException!  This exception is only raised when the RowValidating event fires from the user pressing a Delete button to remove a row.  I can't see why this should happen - any help greatly appreciated!
Here's the code in the RowValidating event:

    DataGridViewRow r = dgvRoles.Rows[e.RowIndex];
    DataRowView drv = r.DataBoundItem as DataRowView;
    DataRowState state = drv.Row.RowState;

The first line above executes fine and populates 'r' with the appropriate DataGridViewRow.  The second line throws the exception, but only when the RowValidating event is fired when I delete rows from the DataGridView.  The delete is fired from this code:

private void btnDeleteRole_Click(object sender, EventArgs e)
{
    if (dgvRoles.SelectedCells.Count > 0)
     dgvRoles.Rows.RemoveAt(dgvRoles.SelectedCells[0].RowIndex);

}

I guess it's something about the row being about to be deleted that causes the problem...?

I've built the form using VS 2005 sp1 and all bindings set up with the designer.  The DataGridView is bound to one table of a DataSet and there is a 'child' DataGridView bound to a related table in the DataSet.

many thanks for any help,

- Rory

Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>