?
Solved

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

Posted on 2009-04-20
3
Medium Priority
?
465 Views
Last Modified: 2012-05-06
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?
0
Comment
Question by:neverrealm
  • 2
3 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 24189001
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.
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 1000 total points
ID: 24189786
Try the following link:

http://windowsclient.net/downloads/folders/applications/entry1285.aspx

Download the library for generating HTML tables. This library can also be passed a Windows Forms DataGrid and generate an HTML table from that object.

Dounload, unzip and view the code - hope it helps.
0
 

Author Comment

by:neverrealm
ID: 24196129
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.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question