Category / Section
How to update the grid while changing the values in underlying datasource in WinForms GridGroupingControl?
2 mins read
Update the grid
By default, the cell values in the grid will be updated when the values in the underlying datasource is changed. In case, the grid is bound to the object collection or Entity framework, the property changes have to be notified to grid. This can be achieved by implementing the INotifyPropertyChanged interface to the class.
C#
/// <summary> /// Summary description for Data with INotifyPropertyChanged interface /// </summary> public class Data : INotifyPropertyChanged { public Data() { } public Data(double cat_Id, string cat_Name, string desc, string sample_Data) { this.cat_Id = cat_Id; this.cat_Name = cat_Name; this.desc = desc; this.sample_Data = sample_Data; } private double cat_Id; public double CategoryID { get { return this.cat_Id; } set { this.cat_Id = value; OnPropertyChanged("CategoryID"); } } //INotifyPropertyChanged member public event PropertyChangedEventHandler PropertyChanged; /// <summary> /// Raise the event when property value changed. /// </summary> /// <param name="Name">Property name</param> public void OnPropertyChanged(String Name) { if (this.PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(Name)); } } }
VB
''' <summary> ''' Summary description for Data with INotifyPropertyChanged interface ''' </summary> Public Class Data Implements INotifyPropertyChanged Public Sub New() End Sub Public Sub New(ByVal cat_Id As Double, ByVal cat_Name As String, ByVal desc As String, ByVal sample_Data As String) Me.cat_Id = cat_Id Me.cat_Name = cat_Name Me.desc = desc Me.sample_Data = sample_Data End Sub Private cat_Id As Double Public Property CategoryID() As Double Get Return Me.cat_Id End Get Set(ByVal value As Double) Me.cat_Id = value OnPropertyChanged("CategoryID") End Set End Property 'INotifyPropertyChanged member Public Event PropertyChanged As PropertyChangedEventHandler ''' <summary> ''' Raise the event when property value changed. ''' </summary> ''' <param name="Name">Property name</param> Public Sub OnPropertyChanged(ByVal Name As String) RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Name)) End Sub End Class
Samples: