Hi,
Currently i'm working on an application working with multiple threads to retrieve and parse data, but I'm running into a problem updating datagridview.
Simple application setup (i'm will use some logic class names to better visualize the structure)
Object Garage
Collection<car> Cars
UserControl GarageView
DataGridView CarTable
</car>
Now i have about 5 threads changing and adding Cars from the collection. In the DataGridView "CarTable" statusses are shown and updated. Currently this works by binding the collection "Cars" as Datasource to "CarTable"
public partial class GarageView : UserControl
{
public GarageView(Garage garage)
{
InitializeComponent();
this.CarTable.DataSource = garage.Cars;
}
}
A car is extended from the INotifyPropertyChanged class so, it can be used as datasource:
public class Car: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
This works, only half, only changes to rows are shown, new cars do not get added. I've found a workaround on the net, but it's ugly, and performs bad.
delegate void UpdateUIDelegate(Collection<car> Cars);
public void UpdateUI(Collection<car> Cars)
{
if (this.InvokeRequired)
{
UpdateUIDelegate d = new UpdateUIDelegate(UpdateUI);
this.Invoke(d, new object[] { Cars });
} else {
this.CarTable.DataSource = null;
this.CarTable.DataSource = Cars;
this.CarTable.Refresh();
this.CarTable.Update();
}
}
</car></car>
Currently there are about 200 records every ten seconds added/changed over a total of 800 rows.
I think I'm going in the proper direction, just can't complete the last bit :/
Any suggestions?
-- Daan