Here's my problem :
I have a DataGridView bound to a BindingList of custom objects. A background thread is constantly updating a value of these objects. The udpates are showing correctly, and everything is fine except for one thing - If you try to edit a different field while the background-updated field is being updated, it loses the entered value. Here is a code sample that demonstrates this behavior:
(for new form, drop a new DataGridView on:)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private BindingList<foo> flist;
private Thread thrd;
private BindingSource b;
public Form1()
{
InitializeComponent();
flist = new BindingList<foo>
{
new foo(){a =1,b = 1, c=1},
new foo(){a =1,b = 1, c=1},
new foo(){a =1,b = 1, c=1},
new foo(){a =1,b = 1, c=1}
};
b = new BindingSource();
b.DataSource = flist;
dataGridView1.DataSource = b;
thrd = new Thread(new ThreadStart(updPRoc));
thrd.Start();
}
private void upd()
{
flist.ToList().ForEach(f=>f.c++);
}
private void updPRoc()
{
while (true)
{
this.BeginInvoke(new MethodInvoker(upd));
Thread.Sleep(1000);
}
}
}
public class foo:INotifyPropertyChanged
{
private int _c;
public int a { get; set; }
public int b { get; set; }
public int c
{
get {return _c;}
set
{
_c = value;
if (PropertyChanged!= null)
PropertyChanged(this,new PropertyChangedEventArgs("c"));
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
}
So, you edit column a or b, you will see that the column c update causes you to lose your entry.
Any thoughts appreciated.