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!