Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a dataGridView and a button in cell index Zero(0) of each row . whenever i click button the belonging row should be deleted from DataGridView.

I am doing ,

C#
private void GvSales_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                // DataGridViewRow row = GvSales.CurrentRow;
                GvSales.Rows.Remove(GvSales.Rows[e.RowIndex]);
            }

}

but it gives Exception...... Operation Cannot be performed in this event handler
Posted
Comments
vinodkumarnie 6-Apr-13 13:32pm    
Didn't u try GridView's default delete option..?

I suggest to handle grid CellClick event (instead of CellEnter) with the following code:
C#
private void GvSales_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex < GvSales.Rows.Count)
                GvSales.Rows.RemoveAt(e.RowIndex);
        }

"+":
without try/catch block
more effective removing
 
Share this answer
 
v2
i am sure it will work fine....
C#
private void GvSales_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == 0)// created column index (delete button)
                {
                    GvSales.Rows.Remove(GvSales.Rows[e.RowIndex]);


                }
            }
            catch
            {

            }
            finally
            {

            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900