我正在开发一个包含GridView的ASP.Net页面,该页面填充了存储过程返回的客户订单。我想做的是动态更改GridView行的背景颜色以指示优先级。
存储过程返回一个指示记录优先级的整数,我想我只需要将整数转换为颜色,然后让GridView行显示它。
“使GridView行显示它”部分是给我带来困难的一部分。
最好的方法是什么?谁能举个例子呢?
提前致谢。
答案 0 :(得分:3)
遵循示例解决方案:
C#: ASPX:
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" >
...
</asp:GridView>
代码隐藏:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int priority = (int)DataBinder.Eval(e.Row.DataItem, "priority");
switch (priority)
{
case 1:
e.Row.BackColor = Drawing.Color.Green;
break;
case 2:
e.Row.BackColor = Drawing.Color.Red;
break;
default:
e.Row.BackColor = Drawing.Color.Black;
break;
}
}
}
VB.Net codebehind:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Select Case e.Row.RowType
Case DataControlRowType.DataRow
Dim priority As Int32 = DirectCast(DataBinder.Eval(e.Row.DataItem, "priority"), Int32)
Select Case priority
Case 1
e.Row.BackColor = Drawing.Color.Green
Case 2
e.Row.BackColor = Drawing.Color.Red
Case Else
e.Row.BackColor = Drawing.Color.Black
End Select
End Select
End Sub