为foreach循环的每次迭代更新Gridview c#.net

时间:2019-04-09 09:47:09

标签: c# gridview foreach

我想知道在c#.net Web应用程序中for-each循环的每次迭代之后如何更新GridView。我已使用for-each循环遍历GridView中的每一行。我想实时更改当前行的BackColor

foreach (GridViewRow row in GridView2.Rows)
{
    // code to make changes per row
    row.Cells[0].BackColor = Color.Red;
    Thread.Sleep(1000);
}

1 个答案:

答案 0 :(得分:1)

在这种情况下,for循环更容易。但是,只要不删除行,就足够了;

for (int i = 0; i < GridView2.Rows.Count; i++)
{
    DataGridViewRow row = GridView2.Rows[i];
    row.DefaultCellStyle.BackColor = Color.Green // my favourite color
}