【问题标题】:Setting gridview row background colour by alternating by row data type通过按行数据类型交替设置gridview行背景颜色
【发布时间】:2017-08-30 13:35:05
【问题描述】:
我有一个 GridView,我在其中列出了客户公司的值。例如,网格列出了公司名称和一个值。我想为 Company 1 的所有行提供 浅灰色 背景颜色,然后为 Company 2 提供 白色的行,然后对于 Company 3 的行返回浅灰色,并像这样交替。
公司 1 12
公司 1 15
公司 1 18
公司 2 25
公司 2 78
公司 2 109
公司 3 66
公司 3 1
这可以简单地在_RowDataBound 事件中完成吗?如果可以,怎么做?
【问题讨论】:
标签:
c#
asp.net
gridview
rowdatabound
alternating
【解决方案1】:
我设法使用了这个问题
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;
【解决方案2】:
在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;
}
}
}