【发布时间】:2011-11-01 04:03:16
【问题描述】:
您好,我正在使用以下代码在验证时更新 DGV:
private void propertyInformationDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (propertyInformationDataGridView.IsCurrentCellDirty && e.ColumnIndex.ToString() != "3")
{
propertyInformationTableAdapter.Update((newCityCollectionDataSet)propertyInformationBindingSource.DataSource);
}
}
这段代码用于更新 DGV 并将值传递给某些表:
private void propertyInformationDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex.ToString() == "3")
{
DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)propertyInformationDataGridView.Rows[e.RowIndex].Cells[3];
DataGridViewRow row = propertyInformationDataGridView.Rows[e.RowIndex] as DataGridViewRow;
System.Data.DataRowView SelectedRowView;
newCityCollectionDataSet.PropertyInformationRow SelectedRow;
SelectedRowView = (System.Data.DataRowView)propertyInformationBindingSource.Current;
SelectedRow = (newCityCollectionDataSet.PropertyInformationRow)SelectedRowView.Row;
if (Convert.ToBoolean(checkCell.Value) == false && propertyInformationDataGridView.IsCurrentCellDirty)
{
DataClasses1DataContext dc = new DataClasses1DataContext();
var matchedCaseNumber = (from c in dc.GetTable<PropertyInformation>()
where c.CaseNumberKey == SelectedRow.CaseNumberKey
select c).SingleOrDefault();
DateTime saveNow = DateTime.Now;
reportsSent newReport = new reportsSent();
newReport.CaseNumberKey = SelectedRow.CaseNumberKey;
dc.reportsSents.InsertOnSubmit(newReport);
matchedCaseNumber.DateFinished = saveNow;
dc.SubmitChanges();
}
}
}
当我单击 value finished 或 cellvalue 3 然后单击不同的单元格(例如不同记录上的单元格值 0)时会发生错误。我明白为什么我会收到错误,但我将如何防止这种情况发生?我应该将代码从验证移动到单击以使错误不会发生还是有其他方法可以解决这个问题?发生错误的原因是因为我正在用这个更新:dc.SubmitChanges();,然后在这里再次更新:
if (propertyInformationDataGridView.IsCurrentCellDirty && e.ColumnIndex.ToString() != "3")
{
propertyInformationTableAdapter.Update((newCityCollectionDataSet)propertyInformationBindingSource.DataSource);
}
我不确定如何在案例完成后强制更新正确的数据集。
错误是:
并发冲突:UpdateCommand 影响了预期的 1 条记录中的 0 条。
在设计器中显示在这里:
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
[global::System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter")]
public virtual int Update(newCityCollectionDataSet dataSet) {
return this.Adapter.Update(dataSet, "PropertyInformation");
谢谢,
韩国
【问题讨论】:
-
始终在您的问题中包含错误消息的措辞。
标签: c# winforms datagridview