none
InvalidOperationException - When ending editing a cell & moving to another cell

    Question

  • If you'd lke to reproduce the problem, I have made a smaller program with explanations:
    http://cid-d62c78e7dba32a61.skydrive.live.com/self.aspx/Errors/InvalidOpartionException.zip

    I made a program in which I wanted to manually update the Data Grid View.
    -I have a Method to Refresh the DGV by clearing it and then reinserting the data.
    -Using the designer, I made an event handler for the DGV's CellEndEdit. Inside the Event Handler, the data gets updated & the DGV's custom refreshing method is called.

    While running the program, whenever I start editing a cell & end it by selcting another one,  an exception is thrown:

    InvalidOperationException
    Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function.

    Visual C#'s debugger marks the line that clears the data : datagridview1.Rows.Clear;
    Also in different versions of my program I used to change the DataSource property instead of manually clearing the data and the same exception is thrown for that line of code (which sets the DataSource).

     Please help me get over this obstacle.
    • Edited by Imiel Thursday, May 21, 2009 2:34 PM
    Thursday, May 21, 2009 2:31 PM

Answers

  • Hi Imiel,

    Yes, as the error indicates, datagridview1.Rows.Clear cause reentrant call to the SetCurrentCellAddressCore function. The problem can be solved by using BeginInvoke method to call the Reresh_dataGridView1 method.

      private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                ...................................................;
                if (toUpdate)
                {
                    foreach(DataRow row in dt.Rows)
                    {
                        if ( (int)row["ID"] == id)
                        {
                            row[columnName] = value;
                        }
                    }
                   
                    this.BeginInvoke(new MethodInvoker(Refresh_dataGridView1));
                }
            }

    I am not sure why you don't use DataBinding techniques. It's very easy to use, and you don't need to write code to manually update the DataGridView.

    Here if you want to bind DataGridView to the datatable, you only need to write the following line of code:

    this.dataGridView1.DataSource = dt;

    If you are interested in databinding, please download DataBinding FAQ to learn more.

    Best regards,
    Bruce Zhou



    Please mark the replies as answers if they help and unmark if they don't.
    • Marked as answer by Bruce.Zhou Wednesday, May 27, 2009 6:34 AM
    Tuesday, May 26, 2009 4:02 AM

All replies

  • Hi Imiel,

    Yes, as the error indicates, datagridview1.Rows.Clear cause reentrant call to the SetCurrentCellAddressCore function. The problem can be solved by using BeginInvoke method to call the Reresh_dataGridView1 method.

      private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                ...................................................;
                if (toUpdate)
                {
                    foreach(DataRow row in dt.Rows)
                    {
                        if ( (int)row["ID"] == id)
                        {
                            row[columnName] = value;
                        }
                    }
                   
                    this.BeginInvoke(new MethodInvoker(Refresh_dataGridView1));
                }
            }

    I am not sure why you don't use DataBinding techniques. It's very easy to use, and you don't need to write code to manually update the DataGridView.

    Here if you want to bind DataGridView to the datatable, you only need to write the following line of code:

    this.dataGridView1.DataSource = dt;

    If you are interested in databinding, please download DataBinding FAQ to learn more.

    Best regards,
    Bruce Zhou



    Please mark the replies as answers if they help and unmark if they don't.
    • Marked as answer by Bruce.Zhou Wednesday, May 27, 2009 6:34 AM
    Tuesday, May 26, 2009 4:02 AM
  • Thanks! That stopped the error. How does it work ? (note: I don't know much about "threads")

    However, now, while in edit mode for a cell, whenever another cell is (to-be-)selected, all the cells from the first cell up to the (to-be-)selected cell are selected (instead of just the to-be-selected cell). Any workaround for this ?

    Also, thanks for the link, I'll check out the FAQ and give Data Binding a try. I don't know enough about Data Binding, and  I wonder how much control will I have over the way data is displayed, and over the way data is manipulated.
    Wednesday, May 27, 2009 6:31 PM
  • Hi Imiel,

    DataBinding is two way binding. Whatever changes you make in the datasource will be reflected in the bound control. Whatever changes you make in the bound control will be propagated to the datasource. So you don't need to write code to capture the change event both on the user control and the datasource, much less to say to write code updating each other.

    I recommend you go through the databinding FAQ, and set up some experiences. You'll be more familar with the databinding technique, and you'll find it is much easier to use databinding to achieve the functionaility you want.

    If you meet something hard to understand, please feel free to let me know.

    Best regards,
    Bruce Zhou
    Please mark the replies as answers if they help and unmark if they don't.
    Thursday, May 28, 2009 1:37 AM