Reputation: 11597
"Every few months I write something in WinForms to remind myself why I hate it" (quote from another op)
I thought that this would be pretty straightforward, but I am lamely failing into coming up with some working code.
I just want to bind a DataGridView to "ANY" convenient 2-Dimensional List/Collection/Banana,
AND update that collection at high frequency by Efficiently (performance, latency) accessing the first dimension as the Key to update the 2nd dimension as the Data. Ideally I needed a Dictionary but it is not "bindable" per se.
Needless to say any change on the binded collection should display on the DGV.
A generalization of the 2 dimensions to N dimensions would be appreciable, as long as I can efficiently acces a "key" on the collection to update the row.
Issue example
var g = this.dataGridView1;
var s = new Dictionary<string, string>();
s.Add("1", "Test1");
s.Add("2", "Test2");
s.Add("3", "Test3");
g.DataSource = s.ToArray();
The problem here is that the toArray() conversion makes the dgv bound to an array and not to the actual dictionary. Therefore any change on the dic won't be reported on the dgv.
Upvotes: 1
Views: 495
Reputation: 1063619
To do this you are going to have to implement ITypedList and create a custom PropertyDescriptor representing each column (usually keeping the "key" or "index" as a field in the descriptor). Then override the GetValue and SetValue to fetch the values from your collection.
I have an example of a transposer that might be useful to set the context - will see if I can find it.
For 2-way notification you will nee to implement IBindingList, and the list-change events.
Upvotes: 1