C# 更改Gridview组行背景颜色
我正在开发一个asp.net应用程序。我有一个gridview,根据订单号对结果进行分组: 我正在使用以下代码:C# 更改Gridview组行背景颜色,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在开发一个asp.net应用程序。我有一个gridview,根据订单号对结果进行分组: 我正在使用以下代码: void GroupGridView(GridViewRowCollection gvrc, int startIndex, int total) { if (total == 0) return; int i, count = 1; ArrayList lst = new ArrayList(); lst.Ad
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();
}
我正在关注这一点。现在我希望备用组的颜色应该不同(而不是备用行)。一组应该是绿色的。然后下一组为白色,然后第三组为绿色,然后为白色,依此类推。如何做到这一点?如果我读对了您的代码,您就不能引用ctrl变量并使用attributes属性吗?例如:
ctrl.Attributes.Add("class", "classname");
我不确定您要将其应用于哪个变量,但这也适用于nextCell TableCell对象
HTH首先将css类添加到您的aspx表单中
<style>
.green { background-color: #00ff21; }
</style>
.green{背景色:#00ff21;}
然后添加额外的代码,如下所示
....
==> 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)
{
....
。。。。
=>intx=0;
对于(i=1;i如果(x%2==0)gvrc[i].CssClass=“绿色”;
=>x++;
TableCell nextCell=gvrc[i]。单元格[startIndex];
if(ctrl.Text==nextCell.Text)
{
....
您可以向gridview添加一列,并为不同的组指定不同的编号,然后在网格的RowDataBound事件处理程序中,您可以根据列编号更改行的颜色
您可以设置行的颜色,如:e、 Row.BackColor=System.Drawing.Color.LightBlue;我需要交替的行来着色,而不是全部
....
==> 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)
{
....