Search code examples
c#winformsdatagridviewcombobox

datagridview combobox column change binding list data --> display automatically


I want to have one:

BindingList XYZ = new BindingList();

and an Item of BindingList has data of a TextBox-Cell and a ComboBox-Cell of one row. If the user now changing data of the ComboBox, he wants to display it immediately on the screen. Can it be achieved with only one List which is directly bind to the datagridview:

DataSource_of_the_datagridview = XYZ;

Could it happen if there is a change on the ComboBox, that the underlying data of the BindingList and furthermore also the displayed data of the datagridview will be changed?

I want to use datagridtextboxcolumn and datagridcomboboxcolumn. What do i should do in my object which represents one row. I have already used this in the class for thes object-rows:

class Fahrzeug : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Solution

  • I would suggest to raise "PropertyChanged" event on any desired property(ies) (in its setter), this will notify in realtime changes to binded objects, and UI will process them.

    We use something like that in a base class: DataGridView.BindingList = new BindingList<object who inherits this>()

    public void SetFieldValue<T>(T field, T value, params string[] propertyNames)
    {
          Foreach var propName In PropertyNames
          {
                NotifyPropertyChanged(propName)
          }
    }
    

    then MAKE SURE you have ONY one REFERENCE on this BindingList, and modify any object, it will update itself in grid...