好的,现在解决了。我忽略了当抛出 DBConcurrencyException 时,布尔值“isManualEditCommit”也需要再次设置为 false。有兴趣的可以看下面的代码:
private void grids_CellEditEnding(object sender, Microsoft.Windows.Controls.DataGridCellEditEndingEventArgs e)
{
try
{
Microsoft.Windows.Controls.DataGrid grid = (Microsoft.Windows.Controls.DataGrid)sender;
gridNumber = Convert.ToInt16(grid.Name.Substring(grid.Name.Length - 1, 1)) - 1;
if (!isManualEditCommit)
{
isManualEditCommit = true;
grid.CommitEdit(Microsoft.Windows.Controls.DataGridEditingUnit.Row, true);
using (SqlConnection con = new SqlConnection(GUI.dictSettings["connectionString"]))
{
con.Open();
SqlCommandBuilder com = new SqlCommandBuilder(adapters[gridNumber]);
adapters[gridNumber].UpdateCommand = com.GetUpdateCommand();
adapters[gridNumber].Update(tables[gridNumber]);
tables[gridNumber].AcceptChanges();
con.Close();
}
isManualEditCommit = false;
}
}
catch (DBConcurrencyException ex)
{
isManualEditCommit = false;
reloadData();
} catch(Exception ex){
isManualEditCommit = false;
reloadData();
}
}