在datagridview单元格值更改后更新数据库记录?
问题描述:
我们曾经使用过一个远端网格,这个网格很容易,但现在我们使用datagridview来处理表单。 我的问题是我需要更新记录,因为它在datagridview中更改,我使用了Cell Leave事件。在datagridview单元格值更改后更新数据库记录?
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1[e.ColumnIndex, e.RowIndex].Value == null)//incase it is a blanked cell
{
MessageBox.Show("Cell changed:" + e.ColumnIndex.ToString() + ":" + e.RowIndex.ToString() + " New Value:" + " null");
}
else
{
MessageBox.Show("Cell changed:" + e.ColumnIndex.ToString() + ":" + e.RowIndex.ToString() + " New Value:" + dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString());
}
}
我通过更改值测试上面的代码,然后按回车键(就像用户目前做的),但它显示未改变的值,但前一个。
我已经完成的解决方法是更新datagridview中的所有记录,问题比先前我需要的时间还要多15秒 - 而且,如果仅更改一条记录,则不需要这么做。
答
好吧,我想我可能已经找到我的答案: 如果我的if-else语句,则细胞变化已被确认
dataGridView1.EndEdit();
之后,改变的单元格值之前把下面的代码看到..看起来不错!
谢谢你对你的这个答案的人!!! :-) – Sylca 2013-01-09 10:00:26