all groups > dotnet windows forms databinding > august 2007 >
You're in the

dotnet windows forms databinding

group:

DataGridView RowValidating IndexOutOfRangeException


DataGridView RowValidating IndexOutOfRangeException imran.a
8/2/2007 6:46:04 AM
dotnet windows forms databinding: 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
datagridview.rows(e.rowindex).

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

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
behavior.

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

Thanks.

RE: DataGridView RowValidating IndexOutOfRangeException imran.a
8/2/2007 6:54:05 AM
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
AddThis Social Bookmark Button