1

I have a Dictionary (Of long, Class). The Class has multiple properties. And I want to view the data of this Dictionary in a DataGridView. So, Simply I used the:

DataGridView1.DataSource = Dictionary.Values.ToArray

Now, the properties inside the Class objects for the records inside the Dictionary are continuously updating (too frequent). And I want to reflect these updates to the DataGridView directly.

Using the DataGridView1.DataSource = Dictionary.Values.ToArray every time is not an efficient way I believe and will take much processing power. How can I make the Dictionary Bindable (as in DataTables for example)? I need this in VB.NET, I tried to use Linq to solve this (Found here https://stackoverflow.com/a/855954/2546348) But still, I didn't see the new updates coming to DataGridView.

Any suggestions? What will be the best approach to implement this?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Ahmad Mousa
  • 745
  • 2
  • 7
  • 18
  • What are you using, WPF or WinForms? – Pavel Anikhouski Nov 11 '19 at 18:33
  • Sorry didn't mention that in the post,, WinForms. – Ahmad Mousa Nov 11 '19 at 19:05
  • 5
    Your class object should implement `INotifyPropertyChanged`. When done, you can bind your `[Dictionary].Values` to a BindingSource, then bind the DGV to the BindingSource (setting the DGV's `.DataSource = [TheBindingSource]`). Any change to the Dictionary values will be immediately reflected in the DGV. – Jimi Nov 11 '19 at 19:37
  • Thanks, @Jimi Your solution worked like a charm. One more question came to my mind. Now how to do same thing but when a new item added to the Dictionary or removed from it? Is there any similar notification that can be raised to let the data source updated based on the latest items (addition and removal on Dictionary) without assigning DataSource for the DGV again... I appreciate your help. – Ahmad Mousa Nov 12 '19 at 13:29
  • Not directly. Even though the ValueCollection exposed by `[Dictionary].Values` is not static (the underlying ValueCollection reference will be updated when you add/remove to/from the Dictionary), it doesn't raise list change notifications (there are means, actually, but I won't suggest any :). You could: 1) Add the new Items to the BindingSource (the `TValue` object, with `BindingSource.Add([object])`), sync the Dictionary when needed. 2) Create your own class that inherits from Dictionary and implements the `IBindingList` interface. 3) Use a DataTable instead of a Dictionary – Jimi Nov 12 '19 at 14:46
  • In point 2), a custom implementation can provide default responses in normal cases, using the default methods of the `Dictionary` implementation, but provide its own form of *ValueCollection* when data-bound, through the `IBindingList` methods and its commonly understood/interpreted list changed notifications. – Jimi Nov 12 '19 at 15:01
  • Thanks again @Jimi. When you said (I won't suggest any). Do you mean I can use either way? Or you don't prefer to use any due to some known issues / drawbacks when using them? I think solution 1 might be the easiest. But will be interested to see an example of suggestion (2) if you have and don't mind. Best :) – Ahmad Mousa Nov 13 '19 at 16:48
  • And BTW @Jimi. I suggest you put your first comment as an answer to be voted as the correct answer since it helped me in my first problem in the post... – Ahmad Mousa Nov 13 '19 at 16:53

1 Answers1

0

Thanks to @jimi's comment, I used the notify property to achieve this... But I needed to put an answer to this question :

Your class object should implement INotifyPropertyChanged. When done, you can bind your [Dictionary].Values to a BindingSource, then bind the DGV to the BindingSource (setting the DGV's .DataSource = [TheBindingSource]). Any change to the Dictionary values will be immediately reflected in the DGV.

Ahmad Mousa
  • 745
  • 2
  • 7
  • 18