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:
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.Code:If Not String.IsNullOrEmpty(dgv.Rows(e.RowIndex).ErrorText.Trim) Then dgv.Rows(e.RowIndex).ErrorText = String.Empty End If
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.




Reply With Quote