6
votes

throwing InvalidOperationException when I changed cell value for update and directly click on menu strip item for open 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- Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.

at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick) at System.Windows.Forms.DataGridView.set_CurrentCell(DataGridViewCell value) at System.Windows.Forms.DataGridView.set_DataSource(Object value)

2
Did u try this code private void dgv_category_CellEndEdit(object sender, DataGridViewCellEventArgs e) { this.BeginInvoke(new MethodInvoker(() => }Govind Tupkar
@GovindTupkar yes, I tried this but not working...Ankush Guhe
Did u try CellLeave event or CellValidating event ?FakeisMe
@FakeisMe, yes I also tried CellLeave event and CellValidating event.Ankush Guhe

2 Answers

0
votes

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

For example

//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
votes

try this one

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; //you don't need to set it to null just remove it

            //try set empty data table 
            DataTable dt = new DataTable();
           dgv_category.DataSource = dt;

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


        }
        catch (InvalidOperationException)
        {
            // exception
        }
    }