如何动态地分配背景色到GridView行? [英] How to dynamically assign a BackColor to a GridView row?
问题描述
我的工作包含一个GridView,其中填充有一个存储过程返回客户的订单一个ASP.Net页面上。我想要做的就是动态地改变在GridView行的背景色来指示优先级。
I'm working on an ASP.Net page containing a GridView, which is populated with customer orders returned by a stored procedure. What I'd like to do is dynamically change the backcolor of the GridView rows to indicate priority.
存储过程返回指示记录优先级的整数,我想我只需要整数转换为一种颜色,然后让在GridView行显示。
The stored procedure returns an integer indicating the records priority, I think I just need to translate the integer to a color, then make the GridView row display it.
在造GridView的行显示它的部分是,公司给了我一个困难时期之一。
The "making the GridView row display it" part is the one that's giving me a hard time.
什么是做到这一点的最好方法是什么?谁能给我一个例子吗?
What's the best way to do this? Can anyone give me an example?
先谢谢了。
推荐答案
经过样品溶液:
C#:
ASPX:
C#: aspx:
<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" >
...
</asp:GridView>
codebehind:
codebehind:
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:
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
这篇关于如何动态地分配背景色到GridView行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!