通过交替行数据类型设置gridview行背景颜色
问题描述:
我有一个GridView,我在这里列出客户公司的值。例如,网格列出了公司名称和价值。我想给一个浅灰背景颜色为所有公司1的行,那么白色为公司2的行,然后返回到浅灰为公司3的行和交替如此。通过交替行数据类型设置gridview行背景颜色
公司112
公司1 15
公司118
公司2 25
公司2 78
公司2 109
公司3 66
公司3 1
这能在_RowDataBound
事件简单地完成,如果又如何?
答
我设法利用这个问题
alternate color gridview rows based on change of cell value asp.net
到C#创建自己的解决方案。所以基本上,创建两个全局变量。然后_RowDatabound内做以下
int customerCompanyID = Convert.ToInt32(dr["IRPCustomerID"]);
if (customerCompanyID != this._trafficSourceGridCurrentCompanyID)
{
if (this._trafficSourceGridGroupingCssClass ==
"BackGround2")
{
this._trafficSourceGridGroupingCssClass = "BackGround1";
}
else
{
this._trafficSourceGridGroupingCssClass = "BackGround2";
}
this._trafficSourceGridCurrentCompanyID = customerCompanyID;
}
e.Row.CssClass = _trafficSourceGridGroupingCssClass;
答
的在RowDataBound
事件中,你可以做这样的事情:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// check if its not a header or footer row
if (e.Row.RowType == DataControlRowType.DataRow)
{
// get value from first cell of gridview
string companyName = e.Row.Cells[0].Text;
switch (companyName)
{
case "Company 1":
e.Row.BackColor = System.Drawing.Color.LightGray;
break;
case "Company 2":
e.Row.BackColor = System.Drawing.Color.White;
break;
case "Company 3":
e.Row.BackColor = System.Drawing.Color.LightGray;
break;
default:
break;
}
}
}