I have a simple test class:
public partial class TEST_CLASS
{
public string IP { get; set; }
public int PORT { get; set; }
}
iList<TEST_CLASS> MY_CLASS = new List<TEST_CLASS>();
And I have the following code to bind the class to DGV:
TEST_CLASS n = new TEST_CLASS();
MY_CLASS.Add(n);
grid_nodes.DataSource = MY_CLASS;
Thread t = new Thread(set1);
t.Start();
Thread t2 = new Thread(set2);
t2.Start();
And The threads that update the class to random values for testing purposes:
public void set1()
{
while (true)
{
Random r = new Random();
MY_CLASS[0].IP = r.Next(999900);
Thread.Sleep(100);
}
}
public void set2()
{
while (true)
{
Random r = new Random();
MY_CLASS[0].PORT = r.Next(999900);
Thread.Sleep(100);
}
}
The problem is that DGV is not getting updated right. I only see the updates each time a click in a cell.
I'm going to have thousands of rows and separate threads updating the MY_CLASS and I need the changes to be displayed 'live' on the DGV.
As far as I understand I need to call DGV.Update() each time I update my class? What would be the best way of doing this, since I'm going to have thousands of threads updating the class constantly? Thanks!
If you use BindingList instead of List, and make your TEST_CLASS implement INotifyPropertyChanged, then the grid will update itself. However you will need to ensure that only happens on the UI thread by some form of marshalling (like SynchronizationContext or grid.Invoke()).