1,105,232 Community Members

Deleting Rows from Datagridview C#

Member Avatar
Exaktor
Light Poster
42 posts since Apr 2010
Reputation Points: 9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

How do you delete rows from a datagridview.
I have a delete button to the right in the datagridview. I want to know how to delete a single row by clicking the delete button.

Member Avatar
Ramy Mahrous
Postaholic
2,189 posts since Aug 2006
Reputation Points: 401 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
Featured
 
0
 

First create delete button in the grid

//I prefer to use the wizard to generate the delete button 
private System.Windows.Forms.DataGridViewButtonColumn Column1;
this.Column1.HeaderText = "delete";
            this.Column1.Name = "Column1";
            this.Column1.Text = "delete";
            this.Column1.UseColumnTextForButtonValue = true;
private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 4)// created column index (delete button)
            {
                dataGridView1.Rows.Remove(dataGridView1.Rows[e.RowIndex]);
                //commit the changes!
            }
        }
Member Avatar
Exaktor
Light Poster
42 posts since Apr 2010
Reputation Points: 9 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I did the way you told me but when i Click the delete button nothing happens.

And what is the 4 for in this code.

if (e.ColumnIndex == 4)// created column index (delete button)
Member Avatar
Ramy Mahrous
Postaholic
2,189 posts since Aug 2006
Reputation Points: 401 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
Featured
 
0
 

created column index (delete button)

Member Avatar
Aung Kaung Hein
Newbie Poster
1 post since Nov 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks. You saved my time.

Member Avatar
unnati.bellare
Newbie Poster
1 post since Feb 2014
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

Hey thanks!! :)

You
Post:
Start New Discussion
Tags Related to this Article