none
DataGridView.Rows.RemoveAt : Operation cannot be performed in this event handler

    Question

  • Hi, 

    I would like to delete empty row from a DataGridView after this row is commited. Here is my code.


    private void dgv_RowValidated(object sender, DataGridViewCellEventArgs e)
            {
                DataGridView oSender = ((DataGridView)sender);

                if (!oSender.Rows[e.RowIndex].IsNewRow)
                {
                    if (IsEmptyRow(oSender.Rows[e.RowIndex]))
                    {                    
                        oSender.Rows.RemoveAt(e.RowIndex);
                    }
                }
            }

    I don't understand why I can't do it because I have an Exception: Operation cannot be performed in this event handler.

    Can anybody help?

    Thanks.

    Thursday, January 10, 2013 1:03 PM

Answers

  • Hi Jerrycod,

    Any modifications (such as remove, add or insert) to the row collection are not supported in this event. It may raise another validation of the rows. If you call this method from either of the eight events, the exception will be thrown. It may cause other exceptions if using threading.

    You can try to use Timer. However it will be not a good idea if it costs a lot of time in Validated event handler. Set the Interval property to 100(milliseconds) for example and call Start() method. Handle the Tick event and Stop the timer.

            void timer_Tick(object sender, EventArgs e)
            {
                dataGridView1.Rows.RemoveAt(rowIndex);
                dataGridView1.Refresh();
                timer.Stop();
            }
    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 15, 2013 9:42 AM
    Moderator

All replies

  • Please check in to the following URL

    http://stackoverflow.com/questions/4960232/operation-cannot-be-performed-in-this-event-handler


    With Thanks and Regards
    Sambath Raj.C
    click "Proposed As Answer by" if this post solves your problem or "Vote As Helpful" if a post has been useful to you
    Happy Programming!

    Thursday, January 10, 2013 2:05 PM
  • I tried everything that is said in your URL, but without success. Someone has an example that works?

    Thanks

    Friday, January 11, 2013 2:09 PM
  • Hi Jerrycod,

    Any modifications (such as remove, add or insert) to the row collection are not supported in this event. It may raise another validation of the rows. If you call this method from either of the eight events, the exception will be thrown. It may cause other exceptions if using threading.

    You can try to use Timer. However it will be not a good idea if it costs a lot of time in Validated event handler. Set the Interval property to 100(milliseconds) for example and call Start() method. Handle the Tick event and Stop the timer.

            void timer_Tick(object sender, EventArgs e)
            {
                dataGridView1.Rows.RemoveAt(rowIndex);
                dataGridView1.Refresh();
                timer.Stop();
            }
    Best regards,

    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 15, 2013 9:42 AM
    Moderator