Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Gridview collections , +
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
Posted 25-Jul-11 4:01am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The Car class implements the INotifyPropertyChanged interface but the actual data source is the Cars object.
 
If I were you, I would create a custom Cars class (not just a collection) and implement INotifyPropertyChanged interface in the Cars class. And whenever a Car was added/changed/removed, I would call the NotifyPropertyChanged method. Even after doing that, you still have to bind a method in your UI code to the Cars.PropertyChanged event and update your GridView there.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

To fully support data binding the collection should implement IBindingList (i.e. BindingList<T>) or INotifyCollectionChanged (e.g. ObservableCollection<T>). You can use a BindingSource but you still have to make sure that it is notified of changes if the underlying collection doesn't do that (and List<T> doesn't).
 
If this is just at startup a progress indicator and a one time hit of all 800 rows might be a better approach, trying to use a data grid which is constantly refreshing is a strange user experience, even if you get all the binding right so scroll position is preserved.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Afzaal Ahmad Zeeshan 256
1 OriginalGriff 251
2 BillWoodruff 240
3 Maciej Los 230
4 Sergey Alexandrovich Kryukov 195
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 25 Jul 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100