ここに私の問題があります: カスタム オブジェクトの BindingList にバインドされた DataGridView があります。バックグラウンド スレッドは、これらのオブジェクトの値を常に更新しています。更新は正しく表示されており、1 つのことを除いてすべて問題ありません。バックグラウンドで更新されたフィールドの更新中に別のフィールドを編集しようとすると、入力した値が失われます。この動作を示すコード サンプルを次に示します (新しいフォームの場合は、新しい DataGridView をドロップします)。
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
}
}
したがって、列 a または b を編集すると、列 c の更新によってエントリが失われることがわかります。
どんな考えでも大歓迎です。