View CellValueChanged event throwing InvalidOperationException data grid

throw InvalidOperationException , when I changed the value of the cell to update and immediately click on the menu item to open a new Winform.

  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 - the operation is invalid because it leads to a repeated call to the SetCurrentCellAddressCore function.

in System.Windows.Forms.DataGridView.SetCurrentCellAddressCore (Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean viaMouseClick) with System.Windows.Forms.DataGridView.set_Curcode.DataGormCellCellCourceCormCellCellCellCellCellCellCellCellCellCellCellCenterCellConnect (object value)

+6
source share
1 answer

Instead of setting the DataSource directly, set the DataSource to BindingSource and then change the BindingSource.DataSource?

for instance

 //create bindingSource in the WinForms Designer 

Then...

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

All Articles