DataGridView bound to BindingList does not refresh when value changed
Solution 1
ListChanged
notifications for item value changes are only raised if the list item type implements theINotifyPropertyChanged
interface.
Taken from: http://msdn.microsoft.com/en-us/library/ms132742.aspx
So my first question would be: Implements your item the INotifyPropertyChanged
properly?
Solution 2
Your datasource should implement INotifyPropertyChanged
for any change in the BindingList to be reflected in the datagridview.
class Books : INotifyPropertyChanged
{
private int m_id;
private string m_author;
private string m_title;
public int ID { get { return m_id; } set { m_id = value; NotifyPropertyChanged("ID"); } }
public string Author { get { return m_author; } set { m_author = value; NotifyPropertyChanged("Author"); } }
public string Title { get { return m_title; } set { m_title = value; NotifyPropertyChanged("Title"); } }
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string p)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(p));
}
}
BindingList<Books> books= new BindingList<Books>();
datagridView.DataSource = books;
Solution 3
Just call myBindingList.ResetBindings()
whenever your data changes!
Solution 4
It sounds as your Change Object notification is not triggered / handled correctly. I personally always use the BindingSource object when binding to a dataGridView.
I would check out the DataGridView FAQ and the DataBinding FAQ and search for object change notification.
If you are using ADO.Net, don't forget the call the .Validate() and .EndEdit() methods.
C Walker
Updated on July 09, 2022Comments
-
C Walker almost 2 years
I have a DataGridView bound to a BindingList (C# Windows Forms). If I change one of the values in an item in the list it does not immediately show up in the grid. If I click on the changed cell, or minimize then maximize the window it updates properly, but I need it to happen automatically.
I had the same problem earlier, but in that situation I had to change the cell's background colour at the same time that the value changed. This caused the cell to refresh correctly.
The only way I can get it to work is...
dataGridView.DataSource = null; dataGridView.DataSource = myBindingList
...but I'd really like to avoid this as it makes the scrollbar pop back to the top, and means that I'd have to set my cell background colours again. Surely there's a better way. I've tried Refresh (as well as refreshing the parent), Update, and Invalidate, but they're not doing what I need.
I've seen this problem mentioned on a few message boards, but haven't seen a working answer to it yet.
-
m.t.bennett over 11 yearsThanks for this, much appreciated
-
curob over 7 yearsThis was perfect. Thank you very much. MSDN should contract with StackOverflow to do their code examples; Microsoft's leave a lot to be desired ("Here is one line of code that doesn't really help you...")
-
C4d over 6 yearsGenius. Saving so much time and code. Haven't known
PropertyChanged
would work in winforms. Thank you very much!