0

我有一个 DataTable,并且我已经为我的更改侦听了DataTable.DefaultView.ListChanged事件,并且在数据更新时,ListChanged 事件在更新现有 DataRow 的相同值时被不正确地触发。

在这里,在使用相同值更新 DataRow 时,ListChanged 事件会通过ListChangedType.ItemChanged触发。但我相信只有在 DataRow 的任何属性被修改时才应该触发该事件。

public DataTable GetTestingDataTable()
{
   DataTable collection = new DataTable();
   collection.Columns.Add("ID", typeof(int));
   collection.Columns.Add("Value", typeof(string));
   collection.Rows.Add(1001, "Value1");
   collection.Rows.Add(1002, "Value2");

   collection.DefaultView.ListChanged += OnDefaultViewListChanged;
   return collection;
}

private void OnDefaultViewListChanged(object sender, ListChangedEventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
    var dataTable = GetTestingDataTable();
    // Lets say value in dataTable.Rows[0]["ID"] is 100.
    // User didn't know what value exists in DataRow and provides 100 as value, which is same from another user interface. (Users application)
    dataTable.Rows[0]["ID"] = 100; // This triggers ListChanged event.
}
4

0 回答 0