Search code examples
c#winformsdatagridview

Delete rows which has a specific value from DataGridView C#


I have a WinForm DataGridView as follows where I need to delete rows which having a specific value in a column

enter image description here

In this case I'm using a textfield to type Code and after clicking "Delete" all the rows which having ITM-000001 under column value. I have no clue where to start.

private void deleteClick(object sender, EventArgs e)
{      
    String code = txtCode.Text;
    // CODE HERE       
}

Solution

  • It's been a long while since I did anything with Windows Forms (oh, the good ol' days), so excuse me if this is a little off, but you should be able to accomplish this with something like...

    for(int v = 0; v < dataGrid1.Rows.Count; v++)
    {
        if(string.Equals(dataGrid1[0, v].Value as string, "ITM-000001"))
        {
            dataGrid1.Rows.RemoveAt(v);
            v--; // this just got messy. But you see my point.
        }
    }