Link to home
Start Free TrialLog in
Avatar of neverrealm
neverrealm

asked on

C# Problem with DataGridView. Throwing an exception after the addition of an event handler.

I have a C# project, in which, although not running properly, actually runs.  I have a DataGridView in this application.

Once I add a handler to the CellValueChanged event of the DataGridView, it throws an IndexOutOfBounds Exception.  Removing the Event Handler, causes the exception to disappear.

The definition for the Event Handler is as follows:


private void rankingsDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            UpdateTopPercentage(rankingsDataGridView[e.ColumnIndex, e.RowIndex].Value.ToString(), false);
        }

It throws the IndexOutOfBounds on Initialize Component on the Form Load on the following line:

resources.ApplyResources(this.Rank, "Rank");
where this.Rank is a DataGridViewTextBoxCell which has been added to the DataGridView.  As said before, merely eliminating the Event Handler rectifies the exception.

What could cause this error?
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

I'm not exactly sure what's causing the problem. However sometimes it is a good idea to disable somehow the event handler while the form is loaded (or some stuff is initialized). Something like:

private void rankingsDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            If(loaded)
               UpdateTopPercentage(rankingsDataGridView[e.ColumnIndex, e.RowIndex].Value.ToString(), false);
        }


loaded is false initially and is set to true at the ened of the form load.
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of neverrealm
neverrealm

ASKER

I ended up moving the sorting out of the cell events.  There's also a track bar that keeps track and edits the cell, so I moved the sorting over to the value changed event of the track bar and that fixes an issue with recurrent CellEdit calls that I was looking to fix with the CellValueChanged event handler.