2016-09-19 60 views
6

抛出InvalidOperationException当我更改单元格值更新并直接单击菜单条项目打开新的Winform。因为它会导致 折返调用SetCurrentCellAddressCore功能数据网格视图CellValueChanged事件抛出InvalidOperationException

private void dgv_category_CellValueChanged(object sender, DataGridViewCellEventArgs e) 
     { 
      DataTable dt = new DataTable(); 
      dt = u.operationOnDataBase(sqlquery_selectCategory, 3); 
      if (dt.Rows.Count > 0) 
      { 
       MessageBox.Show("Category Already Exist..."); 

      } 
      else 
      { 
       u.operationOnDataBase(sqlquery_UpdateCategory, 1); 
       u.SyncMaster("update", "CategoryDetails", 0, Convert.ToInt32(dgv_category[1, e.RowIndex].Value.ToString()));//---------Sync 
      } 

      try 
      { 
       dgv_category.DataSource = null; //here Throwing exception 

       u.operationOnDataBase(sqlquery, 3); 
       dgv_category.DataSource = u.dt; 


      } 
      catch (InvalidOperationException) 
      { 
       // exception 
      } 
     } 

Exception-操作无效。

在System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(的Int32 columnIndex,的Int32 rowIndex位置,布尔setAnchorCellAddress,布尔 validateCurrentCell,布尔throughMouseClick)在 System.Windows.Forms.DataGridView.set_CurrentCell在系统(的DataGridViewCell 值) .Windows.Forms.DataGridView.set_DataSource(对象 值)

+0

难道ü尝试这个代码 '私人无效dgv_category_CellEndEdit(对象发件人,DataGridViewCellEventArgs E) { this.BeginInvoke(新MethodInvoker( ()=> }' –

+0

@GovindTupkar是的,我试过这个,但没有工作... –

+0

你试过CellLeave事件还是CellValidating事件? – FakeisMe

回答

0

代替直接设定DataSource的,数据源设置为一个的BindingSource,然后更改BindingSource.DataSource?

例如

//create bindingSource in the WinForms Designer 

则...

try 
{ 
    dgv_category.DataSource = null; 
    dgv_category.Rows.Clear(); 
} 
catch{} 
bindingSource.DataSource = ut.dt; 
dgv_category.DataSource = bindingSource; 
bindingSource.ResetBindings(true); 
+0

我试过但没有工作它在'dgv_category.DataSource = null;' –

+0

上抛出错误这就是为什么try/catch块。有时候,你可以调用DataSource = null,它会正常工作,有时会抛出错误。捕捉并忽略这些异常可以让其余的代码无论如何都能正常工作。 –

相关问题