Hi
I have a very strange problem. I am using Visual Studio 2005 and C# to develop a Windows application. My database is a MS Access 2003 database.
On my Form I have a DataGridView that is bound to a table in the database. There are also a few DataGridViewComboBoxColumns that are bound to different lookup tables in the database. I specify the BindingSources for all of these using the designer. When I run the app the DataGridView is populated as expected. The DataGridViewComboBoxes also work fine and I can change the values. The database is also updated correctly. So everything seemed fine.
Then I added a Button control to the form by dragging a Button control from the Toolbox onto the Form. Before I added any further code I ran the app again to check something. This time I encountered a problem. After I clicked the Close button in the Controlbox (Top right on the form) the DataGridView gets cleared of data row by row and when all the rows have been cleared the following message appears: "Index 0 does not have a value".
This exception is thrown even if I do not make any changes to the data that is bound to the DataGridView. So even when I run the app and immediately closes the app without doing anything to the data, the exception is thrown.
I tried a debug starting in the FormClosing event and and it seems as if the problem happens when the disposing method is called:
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
When the DataGridView is being disposed its rows are being cleared of the data starting at the topmost row. This is when the exception is thrown.
I am stumped and any advice will be appreciated.
Regards.
Kobus
KobusPosted Jan 6, 2009, 11:50 AM
Hi
I discovered the problem was not with the disposing of the DataGridView, but with another control I have on the form.
I have a separate ComboBox on the form as well and when the selection is changed I change the backcolor of some DataGridViewCells. This is what is causing the exception when the form is closing. I think it still tried to read the values after the DataGridViewRows were being cleared as part of the disposing process.
I added a try .. catch and it works fine now.
Thanks for the replies.
Kobus
shoushiPosted Jan 5, 2009, 3:55 AM
I doubt DataGridView does not dispose itself well, especially you have many combox in it. When you added a button, designer class do something unexpected.
I suggest you delete the form, rebuild it. You have new form, new controls etc.
It maybe VS2005 internal bug.