有一种情况,datagridview不是通过绑定datatable刷新数据的,可以采用
this.datagridview.refreshedit来刷新修改的数据,例如:
//做数据验证,格式化成两位小数,更新不了数据,
private void skinDataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex > -1)
{
DataGridView grid = (DataGridView)sender;
grid.Rows[e.RowIndex].ErrorText = “”;
if ((grid.Columns[e.ColumnIndex].Name == "ylgeren") || ((grid.Columns[e.ColumnIndex].Name == "yldanwei")) || ((grid.Columns[e.ColumnIndex].Name == "yilgeren")) || ((grid.Columns[e.ColumnIndex].Name == "yildanwei")))
{
try
{
// this.skinDataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
string amount = Convert.ToDecimal(e.FormattedValue.ToString()).ToString("N2");// Convert.ToDecimal(e.FormattedValue);
//MessageBox.Show(amount);
this.skinDataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = amount;
this.skinDataGridView1.RefreshEdit(); //解决不能自动刷新修改的数据的关键!!!
//(skinDataGridView1.CurrentRow.DataBoundItem as DataRowView).EndEdit();
//this.skinDataGridView1.Update();
// MessageBox.Show(this.skinDataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
//return;
}
catch
{
e.Cancel = true;
grid.Rows[e.RowIndex].ErrorText = "Please enter a number!";
MessageBox.Show("格式不对!");
return;
}
}
}
}