Data grid view CellValueChanged event throwing InvalidOperationException
Asked Answered
H

2

6

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)

Hynda answered 19/9, 2016 at 13:22 Comment(4)
Did u try this code private void dgv_category_CellEndEdit(object sender, DataGridViewCellEventArgs e) { this.BeginInvoke(new MethodInvoker(() => }Mam
@GovindTupkar yes, I tried this but not working...Hynda
Did u try CellLeave event or CellValidating event ?Gynandrous
@FakeisMe, yes I also tried CellLeave event and CellValidating event.Hynda
C
0

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);
Cobol answered 4/10, 2016 at 3:5 Comment(2)
I tried but not working it throw error on dgv_category.DataSource = null;Hynda
That's why the try/catch block. At times, you can call DataSource = null and it will work fine, other times it throws an error. Catching and ignoring those exceptions allows the rest of the code to work regardless.Cobol
C
0

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
        }
    }
Camporee answered 4/11, 2020 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.