views:

176

answers:

1

Hi,

It will be a stupid question, but a have a Datagridview and a data object in a collection. The datasource of de datagridview is set to this collection (type BindingList).

All data will be displayed and some data in the datagrid will also updatet when I update a property. But only on the first level of the dataobject. Changing a second level property (from a nested object) wll not refreshing the datacolumn of the datagrid with the new value automatic.

Example:

class A
{ 
  public string SampleField;
  public B ClassB;
}
class B
{
  public string FieldB;
}

When the Class B.FieldB is updatet by code, the datagridview doesn't show the change.

I have also try this with the CelLFormatting_Event of the datagrid, but it doesn't trigger the event.

An another solution is to call the BindingList.ResetBindings(), but in this case the datagridview redisplay all with not good looking (you see al rows again building).

So, my question is, what is the best solution to get this solved.

Thank.

+1  A: 

The DGV is not updated because your code doesn't notify the DGV when the value changes. You need to implement the INotifyPropertyChanged in both classes. To do that, you also need to change your fields to properties :

class A : INotifyPropertyChanged
{ 
  private string _sampleField;
  public string SampleField
  {
      get { return _sampleField; }
      set
      {
           _sampleField = value;
           OnPropertyChanged("SampleField");
      }
  };

  private B _classB
  public B ClassB
  {
      get { return _classB; }
      set
      {
           _classB = value;
           OnPropertyChanged("ClassB");
      }
  }

  public event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanged(string propertyName)
  {
      PropertyChangedEventHander handler = PropertyChanged;
      if (handler != null)
          handler(this, new PropertyChangedEventArgs(propertyName);
  }
}

class B : INotifyPropertyChanged
{
  private string _fieldB;
  public string FieldB
  {
      get { return _fieldB; }
      set
      {
           _fieldB = value;
           OnPropertyChanged("FieldB");
      }
  }

  public event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanged(string propertyName)
  {
      PropertyChangedEventHander handler = PropertyChanged;
      if (handler != null)
          handler(this, new PropertyChangedEventArgs(propertyName);
  }
}
Thomas Levesque