CF/VS2005 DataGrid Datasource not really working as other controls do.
First of all, this is Compact Framework, Visual Studio 2005 July CTP.
My databindings is setup like this. I got class full of all sorts of properties. strings, ints, and DataTables. This is added as a project data source so I can drag stuff across to the designer. This created a BindingSource object. All form fields (including 2 data grids) have bindings through this BindingSource. The datasource of this binding source is an ArrayList of the class of all my properties. Makin sense? Sure took me a while to figure out this stuff :P
So now I add/delete/insert objects into this array list. Like BindingSourceObject.Add()
All of the form fields BESIDES the datagrids behave as you would expect, to be updated by the currency manager, however the datagrids are lazy and do not update correctly upon calling BindingSource.Insert() (although Add works). Nota big issue, but then I call all the refresh() reset() methods I can dream up , and it still doesn't update. The datagrids then go onto share objects across the array(eg changing BindingSourceObject.Position to 1 causes the datagrids to access the datatables that belong to position 2 or whatever) They don't seem to grip onto their own datatable that they should be, despite the insert passing a brand new datatable!!) :(:(. So, Any item that has been added with insert causes trouble (the datagrid retains the current datatable instead of the new one passed when Position is changed), but any with Add is Ok. I think delete works ok too.
I'm new to all of this, would appreciate some help.
[1576 byte] By [
Adiraz] at [2007-12-16]
I have made a project file of the problem. To view the form you have to build it at least once apparently. It is done in designer sorta so I want to leave it in its current form. I think it's easiest just to grab the rar than post code.
Link I am having other similar troubles with the datatable bindings to datagrid. If anyone could take a look that'd be great.
the demo....
Warning: Don't delete the last object in the list, itll crash.
It's simple to see the issue. Add a view rows to the datagrid. Then hit Add. You will see that it makes a new datatable and back to 0 rows. Then go back to the table with a few rows. Press Insert. Then select the item that you've just inserted in the combo box. Now that new inserted item will possess the same datatable when it's been passed a fresh one.
To prove that the rest of code works the check box will illustrate how the insert function works correctly for the checkbox but not the datagrid. No binding context refreshes that I have tried work to fix this. Usually I am in favor of cheap work arounds than actual solutions!. I'm not saying theres a bug in CF but I just dont get whats up here. :(
Thanks
OK, I can see the problem, investigating...
Here's a cheap workaround: get rid of all the binding code and simply set grid's data source in comboBox1_SelectedIndexChanged event handler you have anyway:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex != -1)
this.dataGrid1.DataSource = ((Values)vals[comboBox1.SelectedIndex]).Dt;
}
Now you can get rid of Values class as well, just store DataTable’s in the ArrayList or use DataSet as a table collection.
The problem here is: there's no DataSourceChanged event on CF's DataGrid.
That means if you have something bound to this property, this data source will be updated forcefully because biding engine has no way to determine if property has been actually changed.
This update changes 'Dt' property on your classes and you now have 2 different items with the same DataTable inside.
That's why changing index in CB has no effect - items are changing, but 'Dt' property stays the same.
You can fix this problem by making 'Dt' property read only or by setting Binding.DataSourceUpdateMode to DataSourceUpdateMode.Never as follows:
this.dataGrid1.DataBindings.Add(new System.Windows.Forms.Binding("DataSource", this.valuesBindingSource, "Dt", true, System.Windows.Forms.DataSourceUpdateMode.Never));
Thanks. These forums are a great help.
I set them to never in designer and it fixed. My other problems which i did not go into were another issue I solved by setting the datasource to null on closing the dialog with the grids.
Note, if anyone is copying this that the it is not neccesary to handle SelectedIndexChanged on combo box like I did. Changing the item in the combo box changes the datasource position.