I am creating a windows forms application with two basic classes for now (more will be added as the application matures). Class A is the windows forms class and Class B is the underlying logic that drives the widgets in class A. Threads in Class A run methods in Class B. The datagridview is updated in a thread safe manner using the following code is Class A:
Class B has a dataset that is that is global, and has a table and columns added to it in the following way:
Each time the user makes a selection from a combobox, the dataSet is updated in Class B with the following code:
So the first time the user makes a slection from the comboBox, the datagridview is updated successfully. However, evertime after that I get the following error:
System.IndexOutOfRangeException: Index 0 does not have a value.
So for the first pass:
dataGridViewRowsNum = 0
datSetRowsNum = 113 (the number of expected rows in the dataset)
For all other passes:
dataGridViewRowsNum = 623 (this is supposed to be 510, but has 113 extra rows. Why? There might be a data binding problem here.)
datSetRowsNum = 510 (the number of expected rows in the dataset)
So, why is the dataGrideVieww keeping rows from all the previous passes? This is should be cleared for each pass and thus the dataGridView should have only the new data it needs for each pass. The only way I can think about solving this is to have the dataGridview use a local dataSet that is created with each pass, instead of using an update global version of the same dataset. So, I know there must a better way to resolve this - and any help would be greatly appreciated.
- Moved by jack 321 Thursday, July 24, 2008 3:40 AM off topic for C# general
Just as an update, I have cleaned up the code to have an initialize method for the dataGridView. So the method in Class B that is executed in thread is:
And the data is now bound to the dataGridview like this. Please keep in mind that a local copy of the dataset used datagridview is now set as a copy of the dataset from Class B.
This works fine, but I am creating a new dataset each time the datagridview is updated, and I really did not want to do that. I just wanted to use the same global dataset and to update the desired table in the dataset as needed to update the datagridview. I tried to do this in the commented code, but it failed. I have also tried to use the update and refresh methods for the data grid view, and that failed as well. So, got any ideas? Does the datagrdview have a bug, or do I not know how ot code very well?
Please withdraw this post since no one is responding.