【问题标题】:Datagridview concurrency violationsDatagridview 并发冲突
【发布时间】: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


【解决方案1】:

您究竟为什么要结合强类型数据集和 LINQ to SQL?正如您可能已经猜到的那样,他们正在尝试更新相同的数据,因此第二个框架遇到了 ConcurrencyViolation。

【讨论】:

  • 我知道发生了什么问题是如何解决它?
  • 要么对所有内容使用相同的框架,要么在每次另一个更新数据库时从数据库中重新加载每个框架。
  • 好吧,我只知道如何使用 Linq to SQL 更新表,那么我该怎么做呢?唯一需要更新的值是属性信息表中的 DateFinished 到 DateTime.Now()。
  • newCityCollectionDataSet.PropertyInformationRow 没有 DateFinished 属性?
  • 如果我设置 SelectedRow.DateFinished = DateTime.Now;它不选中复选框。它运行代码但忽略了我猜测的复选框,因为它仍在尝试一次更新数据集的 2 个版本。如果我删除该行,它工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-18
  • 1970-01-01
  • 2023-03-29
  • 2018-07-10
相关资源
最近更新 更多