2011-10-31 72 views
1

你好我使用下面的代码来更新DGV在验证:DataGridView的并发冲突

 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(); 

      } 


     } 
    } 

会发生什么是当我单击值完成或cellvalue 3然后单击不同的单元格,例如在不同的记录上的单元格值0我得到一个错误。我明白为什么我得到这个错误,但我怎么去防止这个呢?我应该将代码从验证移到点击以便不会发生错误,或者有其他方法可以解决这个问题吗?出现错误的原因是因为我与此更新:dc.SubmitChanges();,然后在这里再次更新:

if (propertyInformationDataGridView.IsCurrentCellDirty && e.ColumnIndex.ToString() != "3") 
     { 
      propertyInformationTableAdapter.Update((newCityCollectionDataSet)propertyInformationBindingSource.DataSource); 
     } 

我不知道如何强制正确的数据集进行更新的情况下完成之后。

错误是:

并发冲突:在更新命令影响的预期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"); 

感谢,

+3

始终在您的问题中包含错误消息的措词。 –

回答

0

为什么你到底是结合强类型数据集和LINQ to SQL的?正如您可能已经猜到的那样,他们正在尝试更新相同的数据,因此第二个出现的框架正受到ConcurrencyViolation的冲击。

+0

我明白发生了什么问题是我该如何解决? – korrowan

+0

对于所有内容都使用相同的框架,或者每次更新数据库时从数据库中重新加载每个框架。 – sq33G

+0

以及我只知道如何更新Linq到SQL的表,所以我会如何做到这一点?唯一需要更新的值是DateFinished到propertyInformation表中的DateTime.Now()。 – korrowan