ColumnChanging 會發生在欄位資料值進行改變時(跟dbgrid的ColumnChanging 意思差很多~)
可以在此事件中,抓到那個爛位發生資料值變更,且新值與舊值都抓得到
private static void DataTableColumnChanging()
{
DataTable custTable = new DataTable("Customers");
// add columns
custTable.Columns.Add("id", typeof(int));
custTable.Columns.Add("name", typeof(string));
custTable.Columns.Add("address", typeof(string));
// set PrimaryKey
custTable.Columns["id"].Unique = true;
custTable.PrimaryKey = new DataColumn[] { custTable.Columns["id"] };
// add a ColumnChanging event handler for the table.
custTable.ColumnChanging += new
DataColumnChangeEventHandler(Column_Changing);//把事件綁上去
// add ten rows
for (int id = 1; id {
custTable.Rows.Add(
new object[] { id, string.Format(
"customer{0}", id), string.Format("address{0}", id) });
}
custTable.AcceptChanges();
// change the name column in all the rows
foreach (DataRow row in custTable.Rows)
{
row["name"] = string.Format("vip{0}", row["id"]);//重點在這裡,這裡會觸發custTable.ColumnChanging 事件
}
}
private static void Column_Changing(object sender,
DataColumnChangeEventArgs e)
{
string showInfo=
"Column_Changing Event: name="+e.Row["name"]+
"; Column="+e.Column.ColumnName+"; proposed name="+ e.ProposedValue;
MessageBox.Show(showInfo);
}