How to programmatically set cell value in DataGridView?

前端 未结 14 891
無奈伤痛
無奈伤痛 2020-11-30 02:05

I have a DataGridView. Some of the cells receive their data from a serial port: I want to shove the data into the cell, and have it update the underlying bound object.

14条回答
  •  庸人自扰
    2020-11-30 02:47

    Just like @Thomas said, the element you want to change must implement INotifyPropertyChanged. But, datasource is also important. It has to be BindingList, which you can create easily from List.

    Here is my example - data source is at first DataTable, which I transfer to List and then create BindingList. Then I create BindingSource and use BindingList as DataSource from BindingSource. At last, DataSource from DataGridView uses this BindingSource.

     sp_Select_PersonTableAdapter adapter = new sp_Select_PersonTableAdapter();
    
     DataTable tbl = new DataTable();
     tbl.Merge(adapter.GetData());
    
     List list = tbl.AsEnumerable().Select(x => new Person
     {
         Id = (Int32) (x["Id"]),
         Ime = (string) (x["Name"] ?? ""),
         Priimek = (string) (x["LastName"] ?? "")
     }).ToList();
    
     BindingList bindingList = new BindingList(list);
    
     BindingSource bindingSource = new BindingSource();
     bindingSource.DataSource = bindingList;
     dgvPerson.DataSource = bindingSource;
    

    What is also very important: each class's member setter must call OnPropertyChanged(). Without that, it won't work. So, my class looks like this:

    public class Person : INotifyPropertyChanged
        {
            private int _id;
            private string _name;
            private string _lastName;
    
            public int Id
            {
                get { return _id; }
                set
                {
                    if (value != _id)
                    {
                        _id = value;
                        OnPropertyChanged();
                    }
                }
            }
            public string Name
            {
                get { return _name; }
                set
                {
                    if (value != _name)
                    {
                        _name = value;
                        OnPropertyChanged();
                    }
                }
            }
            public string LastName
            {
                get { return _lastName; }
                set
                {
                    if (value != _lastName)
                    {
                        _lastName= value;
                        OnPropertyChanged();
                    }
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;
    
            [NotifyPropertyChangedInvocator]
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
    
        }
    

    More on this topic: http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

提交回复
热议问题