我该如何在DataGridView中删除指定索引的行?例如,如果我想要删除索引为[2]的行,应该怎么做?
我尝试过以下代码:
for (int i = 0; i < dg1.Rows.Count; i++)
{
if (i == 2)//if iteration has reached index 2
{
dg1.Row[i].Delete;
}
}
我正在使用 Winforms
我该如何在DataGridView中删除指定索引的行?例如,如果我想要删除索引为[2]的行,应该怎么做?
我尝试过以下代码:
for (int i = 0; i < dg1.Rows.Count; i++)
{
if (i == 2)//if iteration has reached index 2
{
dg1.Row[i].Delete;
}
}
我正在使用 Winforms
Rows
集合本身上使用 RemoveAt
:dg1.Rows.RemoveAt(i);
` 标签。
dg1.Rows.RemoveAt(i);
一切都会正常运作。我以前做过这个。
在删除行后刷新DataGridView或使用Remove方法
for (int i = 0; i < dg1.Rows.Count; i++)
{
if (i == 2)//if iteration has reached index 2
{
dg1.Row[i].Delete;
dg1.Refresh();
}
}
或者
for (int i = 0; i < dg1.Rows.Count; i++)
{
if (i == 2)//if iteration has reached index 2
{
dg1.Row[i].Remove();
}
}