How to delete a selected DataGridViewRow and update a connected database table?

385,980

Solution 1

This code removes selected items of dataGridView1:

 private void btnDelete_Click(object sender, EventArgs e)
 {
     foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
     {
         dataGridView1.Rows.RemoveAt(item.Index);
     }
 }

Solution 2

private void buttonRemove_Click(object sender, EventArgs e)
{
    foreach (DataGridViewCell oneCell in dataGridView1.SelectedCells)
    {
        if (oneCell.Selected)
            dataGridView1.Rows.RemoveAt(oneCell.RowIndex);
    }
}

Removes rows which indexes are in selected cells. So, select any cells, and their corresponding rows will be removed.

Solution 3

I have written the following code, please take a look:

foreach (DataGridViewRow row in dataGridView1.SelectedRows)
    if (!row.IsNewRow) dataGridView1.Rows.Remove(row);

using the Index of the selected row still could work; see if the code below will do the trick:

int selectedCount = dataGridView1.SelectedRows.Count;           
while (selectedCount > 0)
{
    if (!dataGridView1.SelectedRows[0].IsNewRow)
        dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
    selectedCount--;
}

I hope this helps, regards.

Solution 4

private void btnDelete_Click(object sender, EventArgs e)
{
    if (e.ColumIndex == 10)// 10th column the button
    {
        dataGridView1.Rows.Remove(dataGridView1.Rows[e.RowIndex]);
    }                
}

This solution can be delete a row (not selected, clicked row!) via "e" param.

Solution 5

maybe you can use temp list for delete. for ignore row index change

<pre>
private void btnDelete_Click(object sender, EventArgs e)
{
    List<int> wantdel = new List<int>();
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if ((bool)row.Cells["Select"].Value == true)
        wantdel.Add(row.Index);
    }

    wantdel.OrderByDescending(y => y).ToList().ForEach(x =>
    {
        dataGridView1.Rows.RemoveAt(x);
    });           
}
</pre>

Share:
385,980
Woody
Author by

Woody

I am a Sr. Information Security Engineer for a small company in the United States. Currently, my interests are in small security applications written in C#. I consider myself a Jr.-level C# Developer... so I have a looooong way to go. :)

Updated on January 04, 2022

Comments

  • Woody
    Woody over 2 years

    I have a DataGridView control on a Windows Forms application (written with C#).

    What I need is: when a user selects a DataGridViewRow, and then clicks on a 'Delete' button, the row should be deleted and next, the database needs to be updated using table adapters.

    This is what I have so far:

    private void btnDelete_Click(object sender, EventArgs e)
    {
        if (this.dataGridView1.SelectedRows.Count > 0)
        {
            dataGridView1.Rows.RemoveAt(this.dataGridView1.SelectedRows[0].Index);
        }                
    }
    

    Furthermore, this only deletes one row. I would like it where the user can select multiple rows.

  • Woody
    Woody over 14 years
    thanks... the main problem I'm having now is saving to the database using tableadapters.
  • serge_gubenko
    serge_gubenko over 14 years
    you need to provide more details on what is not working for you; I bet the best response you would get by starting a new question with a code snip which is causing troubles
  • moltenform
    moltenform over 9 years
    In general it's not always safe to be modifying an object related to the object we are iterating over, also the indices might not be updated.
  • Grungondola
    Grungondola almost 8 years
    Best practices say to use a for loop instead of a foreach loop and iterate backward from the end. This helps you to preserve your index and avoid issues while editing during your iterations.
  • User
    User almost 8 years
    @Grungondola you should make a separate answer for this
  • Volkan Güven
    Volkan Güven over 7 years
    Why not use foreach instead? Ref to @Navid Farhadi's answer.
  • Maksim Simkin
    Maksim Simkin over 7 years
    Could you please explain your answer, just naked code is not enough
  • Grofman
    Grofman over 4 years
    Just do code and it will do the job. Example: prntscr.com/p3208c
  • Peter
    Peter almost 4 years
    Welcome to SO - it may be useful to add some commentary about what the code you posted is doing to help the OP understand it.