DataGridView RowValidating IndexOutOfRangeException

DataGridView RowValidating IndexOutOfRangeException

Post by aW1yYW4uY » Fri, 03 Aug 2007 22:46:04

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.

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!



DataGridView RowValidating IndexOutOfRangeException

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

I forgot to mention one thing.

I have also managed to narrow down the conditions that cause the error and
it seems to surface if i have a filter set for a particular 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.