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

Posted on 2009-04-20
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?
Question by:neverrealm
    LVL 29

    Expert Comment

    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)
                   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.
    LVL 29

    Accepted Solution

    Try the following link:

    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.

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    758 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now