Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete a row with specified index in datagridview

How do I delete a row with a specified index in datagridview? So if I want to delete the row with index [2], then how can I do that?

I have tried:

for (int i = 0; i < dg1.Rows.Count; i++)
{
    if (i == 2)//if iteration has reached index 2
    {
        dg1.Row[i].Delete;
    }
}

I am using Winforms

like image 674
Edward Karak Avatar asked Oct 17 '25 18:10

Edward Karak


2 Answers

You need to use RemoveAt on the Rows collection itself:

dg1.Rows.RemoveAt(i);
like image 146
Mike Perrenoud Avatar answered Oct 20 '25 07:10

Mike Perrenoud


Use the

dg1.Rows.RemoveAt(i);

and everything will work. I have done this before.

like image 20
Edward Karak Avatar answered Oct 20 '25 08:10

Edward Karak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!