【发布时间】:2017-01-19 13:49:24
【问题描述】:
我想知道是否有办法将 GridView 中的所有空单元格着色为橙色。我的 GridView 中的列是动态生成的。任何帮助表示赞赏。
谢谢!
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the rowtype is a datarow
if (e.Row.RowType == DataControlRowType.DataRow)
{
//loop all the cells in the row
for (int i = 0; i < e.Row.Cells.Count; i++)
{
int value = 0;
//try converting the cell value to an int
try
{
value = Convert.ToInt32(e.Row.Cells[i].Text);
}
catch
{
}
//check the value and set the background color
if (value == "")
{
e.Row.Cells[i].BackColor = Color.Green;
}
else
{
e.Row.Cells[i].BackColor = Color.White;
}
}
}
}
【问题讨论】:
-
OnRowDataBound 事件是您的起点。在那里,您可以遍历所有单元格并检查它们的值。
-
看HERE。
-
你能告诉我你是怎么做的吗?
-
我的单元格没有值。你如何过滤这些?
-
另外,我不确定我会得到多少列,因为 gridview 是动态的。
标签: c# asp.net .net gridview cells