[RESOLVED] [2005] DataGridView CellValidated Event Issues
I have a DataGridView bound to a DataSet on my form. When the user clicks on the Cancel button, I call DataSet.Clear and reset the various parts of the form to get it ready for new data input.
The problem is that if the user is in the DGV when Cancel is clicked and DataSet.Clear is called, then the CellValidated event fires and gives an IndexOutOfRangeException.
The following exception occurred in the DataGridView:
System.IndexOutOfRangeException: Index 0 does not have a value.
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowindex)
This is firing on the first line of the following block of code:
Code:
If Not String.IsNullOrEmpty(dgv.Rows(e.RowIndex).ErrorText.Trim) Then
dgv.Rows(e.RowIndex).ErrorText = String.Empty
End If
I had this same thing happen in a previous app and I just created an IsResetting form-level boolean. On click of Cancel, I would set it to True and then any routines that gave problems, I would have them watch the IsResetting and if True, don't process the routine.
I would rather not go the route of the IsResetting boolean. I am hoping that there is some way of looking at the control or the DataSet and knowing what state everything is in.
EDIT:
I have determined that I can access the data in the cells of the DGV. The error happens when I look at the ErrorText property for the DGV Row object.
Re: [2005] DataGridView CellValidated Event Issues
You can check the row count on your datatable or DGV, and if it's > 0 then you run the code.
Re: [2005] DataGridView CellValidated Event Issues
I actually did. RowCount = 2. I can look at all sorts of information about the DGV in break mode, but I cannot look at the ErrorText property.
Re: [2005] DataGridView CellValidated Event Issues
An update in case anyone else runs into this.
I replaced:
Code:
If Not String.IsNullOrEmpty(dgv.Rows(e.RowIndex).ErrorText.Trim) Then
dgv.Rows(e.RowIndex).ErrorText = String.Empty
End If
with
Code:
dgv.Rows(e.RowIndex).ErrorText = String.Empty
and everything works.
I tested various scenarios, and just not looking at ErrorText prior to clearing it was enough to stop the IndexOutOfRangeException.
Weird, but now it works. I cannot imagine how many hours I spent nailing this one down.