D
Don Idioto Grande
Guest
Hi!
Im struggling with a task that I thought would be not much harder than anything else Ive done so far (Im new to C# and .NET), however, reading through hundreds of subjects on the forums, I still cant seem to get i right...
Im having a DataGridView where two columns (in the future three) consists of comboboxes that should be related to each other in a parent/child way. The first is product main categories and the second is subcategories.
I have one table with the main categories and their id:s and one table with all the subcategories and their id:s (both in the same dataset) and a datarelation between them.
The comboboxes are set up against two bindingsources. Maincategories bindingsource has the dataset as its datasource and the maincategories table as its datamember. The subcategories bindingsource has the maincategories bindingsource as its datasource and the datarelation maincat_subcat as its datamember.
So far all is good. The items in the lists are filtered fine. To take care of the data error exception that occurs if the user selects a main cat and a subcat and then changes the maincat so that the subcat is no longer valid i subscribe to the EditingControlShowing event in the DataGridView and then if the caller is a ComboBox I subscribe to the SelectionChangeCommitted event for the DataGridViewComboBoxEditingControl and sets the subcat value to null.
Now comes the problem. The gridview should be populated from a datatable and the comboboxes set to the current values in the table, allowing the user to change them if necessary. I do this by setting the Value property of the cells like this
dataGridView1.Rows.Cells["MainCat"].Value = Convert.ToInt32(selectedRowsDT.Rows["MainCat"]);
but at runtime I get a DataError ArgumentException. One might think that I put in inaccurate values but I dont think thats the case. I have checked that the values are in corespondance to each others and if I skip the datarelation between the tables, the comboboxcells are populated fine.
Im beginning to think that the probleme is about the bindingsource not being updated in some way and that the Position property for the bindingsource would solve it. However, I cant figure out where to get the info to put in there, the SelectedIndex property is only available from the DataGridViewComboBoxEditingControl and not from the DataGridViewComboBoxCell as I can see....
I might add that when I step through the code in the debugger I dont get the exception until the grid is filed and the form is showed... (its a form launched from another form)
Any sugestions how to solve this? Ive tried asking about this on other forums but so far not a single answer... hoping for better luck here!
Kind regards
Johan
Ps. Would it be easier to solve this with WPF?
Continue reading...
Im struggling with a task that I thought would be not much harder than anything else Ive done so far (Im new to C# and .NET), however, reading through hundreds of subjects on the forums, I still cant seem to get i right...
Im having a DataGridView where two columns (in the future three) consists of comboboxes that should be related to each other in a parent/child way. The first is product main categories and the second is subcategories.
I have one table with the main categories and their id:s and one table with all the subcategories and their id:s (both in the same dataset) and a datarelation between them.
The comboboxes are set up against two bindingsources. Maincategories bindingsource has the dataset as its datasource and the maincategories table as its datamember. The subcategories bindingsource has the maincategories bindingsource as its datasource and the datarelation maincat_subcat as its datamember.
So far all is good. The items in the lists are filtered fine. To take care of the data error exception that occurs if the user selects a main cat and a subcat and then changes the maincat so that the subcat is no longer valid i subscribe to the EditingControlShowing event in the DataGridView and then if the caller is a ComboBox I subscribe to the SelectionChangeCommitted event for the DataGridViewComboBoxEditingControl and sets the subcat value to null.
Now comes the problem. The gridview should be populated from a datatable and the comboboxes set to the current values in the table, allowing the user to change them if necessary. I do this by setting the Value property of the cells like this
dataGridView1.Rows.Cells["MainCat"].Value = Convert.ToInt32(selectedRowsDT.Rows["MainCat"]);
but at runtime I get a DataError ArgumentException. One might think that I put in inaccurate values but I dont think thats the case. I have checked that the values are in corespondance to each others and if I skip the datarelation between the tables, the comboboxcells are populated fine.
Im beginning to think that the probleme is about the bindingsource not being updated in some way and that the Position property for the bindingsource would solve it. However, I cant figure out where to get the info to put in there, the SelectedIndex property is only available from the DataGridViewComboBoxEditingControl and not from the DataGridViewComboBoxCell as I can see....
I might add that when I step through the code in the debugger I dont get the exception until the grid is filed and the form is showed... (its a form launched from another form)
Any sugestions how to solve this? Ive tried asking about this on other forums but so far not a single answer... hoping for better luck here!
Kind regards
Johan
Ps. Would it be easier to solve this with WPF?
Continue reading...