DataGridView RowValidating IndexOutOfRangeException

DataGridView RowValidating IndexOutOfRangeException

Post by aW1yYW4uY » Fri, 03 Aug 2007 22:54:12

Hi all,
I am having a problem with inserting rows in to my datagridview control. The
datagridview is bound to a bindingsource with a filter set. If i try and add
a row to the datagridview while the filter is set i get an
IndexOutOfRangeException specifying that the index of the row i have just
added does not exist. I have managed to narrow down the conditions that cause
the error and it seems to surface if i have a filter set for a paricular
column value. For example if I have a filter set to name = bob and attempt to
add a row but specify name = bill i will get the IndexOutOfRangeException.

The exception occurs when i try and access the datagridview.currentrow
property in my rowvalidating event handler, or alternatively

void ValidateRow(object sender, DataGridViewCellCancelEventArgs e)
DataGridView tmpDGV = ((DataGridView)sender);
if (tmpDGV.IsCurrentRowDirty && tmpDGV.CurrentRow != null)
if (Properties.Settings.Default.validation &&
MessageBox.Show("invalid format.", "Segment Format
Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
e.Cancel = true;
catch (IndexOutOfRangeException ex)
//filtered row addition will throw this

If i debug this code and step through it i can see that when i have finished
editing the new record and hit enter the row validating event handler fires.
This works fine at first. The row validating event handler then fires a
second time and this is when the exception is thrown. I can catch the
exception and it would seem that the row is successfully added to the
datagridview however I'd like to know what it is that is causing this

Any help would be greatly appreciated as I am stumped on this one!



1. DataGridView (.NET 2.0 Windows Forms) Text wrapping in DataGridView Items

2. Refreshing Linq query to update second DataGridView when position in first DataGridView changes

This seems to be something so simple that none of the hundred-odd tutorials
and forum threads that I have looked at (:-)) apparently thinks it's a

In a nutshell, I have two DataGridViews. Each has a BindingSource (and a
BindingNavigator). The first view populates successfully based upon a Linq
to SQL query in the Form Load (that is, the query is assigned to the
DataSource of that view's BindingSource, and the BindingSource is assigned
to the DataSource of the view). Or:

var personsQuery = from p in pim.persons
select p;

personBindingSource.DataSource = personsQuery;
personDataGridView.DataSource = personBindingSource;

The second view (which is actually address data accessed through a
person_address join table) is set up like:

addressBindingSource.DataSource = personsQuery;
addressDataGridView.DataSource = addressBindingSource;
addressDataGridView.DataMember = "addresses";

The method "addresses" in class "person" handles the join table. In any
case, when I run the app, the second DataGridView successfully populates
with the rows of the address table that correspond to the first row (first
"person") in the first DataGridView. So far so good.

Problem being, if I select another row in the first DataGridView (in this
example, a different "person"), I'd like to see the address or addresses
corresponding to that selected person. I have a SelectionChanged event
handler set up on the personDataGridView, and it is successfully reporting
the Position changes in the personBindingSource. I'll be damned if I can
figure out how to get the addressBindingSource to update, that is, to
effectively re-run the person.addresses method for a different person.

Any suggestions?


3. dataGridView.Rows.Count and datagridview.RowCount

4. Binding List<T> to a DataGridView = DataGridView Empty

5. Clearing selection in DataGridView when user clicks outside the DataGridView control

6. DataGridView into another DataGridView

7. Class Inherits DataGridView, Errors: Rows(0).DataGridView

8. Copy from datagridview and paste to second datagridview

9. Refreshing datagridview contents after a failed delete in bound datagridview

10. Datagridview - Writing Journal Transactions for a Datagridview

11. dataGridView.Rows.Count and datagridview.RowCount

12. IndexOutOfRangeException when CF is trying to consume webservice

13. Bizarre IndexOutOfRangeException during Fill

14. datagrid IndexOutOfRangeException

15. datatable, 'System.IndexOutOfRangeException'