我有一些代码可以更改 GridView
中特定标签的背景颜色,而且效果很好。
protected void HighLight_Hours(Label Quarter)
{
Int32 Hours;
Int32.TryParse(Quarter.Text, out Hours);
switch (Hours)
{
case 0:
Quarter.BackColor = Color.Red;
break;
case 1:
Quarter.BackColor = Color.Yellow;
break;
case 2:
Quarter.BackColor = Color.LightGreen;
break;
}
}
但是不是为我的网格中的每个标签调用我的函数(有很多,一天中每 15 分钟一个标签)有没有一种方法可以遍历 GridView
的所有内容> 并相应地设置颜色?
最佳答案
尝试这样的事情:
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// read in all controls of a row
foreach (var control in e.Row.Controls)
{
// check if the control is a label
if (control is Label)
{
// call your function and cast the control to a Label
HighLight_Hours((Label) control);
}
}
}
}
关于c# - 将背景颜色应用于 GridView 中的所有标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11299457/