1.保存datagridview的某一行已经修改的数据时,可以使用CellEnter事件和CellLeave事件。思路,在CellEnter事件中获取旧的文本,在CellLeave事件中获取修改的值。然后再保存。
代码如下:
View Code
1 private void dataGridView1_CellLeave( object sender, DataGridViewCellEventArgs e) 2 { 3 var dgv = sender as DataGridView; 4 this .dataGridView1.EndEdit(); 5 string cellNewValue = Convert.ToString(dgv[e.ColumnIndex,e.RowIndex].Value); 6 7 if ( ! cellNewValue.Equals(cellOldValue)) 8 { 9 MessageBox.Show( string .Format( " oldValue:{0},newValue{1} " ,cellOldValue,cellNewValue)); 10 } 11 12 13 } 14 15 private void dataGridView1_CellEnter( object sender, DataGridViewCellEventArgs e) 16 { 17 var dgv = sender as DataGridView; 18 cellOldValue = Convert.ToString(dgv.CurrentCell.Value); 19 20 }