更改Gridview组的行背景颜色
本文关键字:背景 颜色 Gridview 更改 | 更新日期: 2023-09-27 18:08:14
我正在做一个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变量和使用属性?例如:
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)
{
....