[英]Changing Gridview groups row background color
我正在一个asp.net应用程序上。 我有一个gridview,根据订单号将结果分组:
我正在使用此代码:
void GroupGridView(GridViewRowCollection gvrc, int startIndex, int total)
{
if (total == 0) return;
int i, count = 1;
ArrayList lst = new ArrayList();
lst.Add(gvrc[0]);
var ctrl = gvrc[0].Cells[startIndex];
for (i = 1; i < gvrc.Count; i++)
{
TableCell nextCell = gvrc[i].Cells[startIndex];
Label lblNextOrderID = nextCell.FindControl("lblOrderID") as Label;
Label lblOrderID = ctrl.FindControl("lblOrderID") as Label;
if (lblOrderID.Text == lblNextOrderID.Text)
{
count++;
nextCell.Visible = false;
lst.Add(gvrc[i]);
}
else
{
if (count > 1)
{
ctrl.RowSpan = count;
ctrl.VerticalAlign = VerticalAlign.Middle;
GroupGridView(new GridViewRowCollection(lst), startIndex + 1, total - 1);
}
count = 1;
lst.Clear();
ctrl = gvrc[i].Cells[startIndex];
lst.Add(gvrc[i]);
}
}
if (count > 1)
{
ctrl.RowSpan = count;
GroupGridView(new GridViewRowCollection(lst), startIndex + 1, total - 1);
}
count = 1;
lst.Clear();
}
并这样称呼它:
gvOrderHistory.DataSource = gridDataSource;
gvOrderHistory.DataBind();
GroupGridView(gvOrderHistory.Rows, 0, 1);
我正在跟踪此链接 。 现在,我希望备用组的颜色应该不同(而不是备用行)。 一组应该是绿色的。 然后下一组白色,然后第三组再次绿色,然后白色,依此类推。 这个怎么做 ?
您可以将一列添加到gridview并为不同的组提供不同的编号,然后在网格的RowDataBound事件处理程序中,可以基于列编号更改行的颜色。
您可以像这样设置行的颜色:
e.Row.BackColor = System.Drawing.Color.LightBlue;
如果林先生正确阅读您的代码,难道您不可以仅引用ctrl变量并使用attributes属性吗? 例如:
ctrl.Attributes.Add("class", "classname");
我不确定您要将此变量应用到哪个变量,但这也可以用于nextCell TableCell对象。
HTH
首先向您的aspx表单添加一个CSS类
<style>
.green { background-color: #00ff21; }
</style>
然后添加额外的代码,如下所示
....
==> int x = 0;
for (i = 1; i < gvrc.Count; i++)
{
==> if(x % 2 == 0) gvrc[i].CssClass = "green";
==> x++;
TableCell nextCell = gvrc[i].Cells[startIndex];
if (ctrl.Text == nextCell.Text)
{
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.