简体   繁体   中英

Changing Gridview groups row background color

I am working on an asp.net application. I have a gridview where I am grouping the results based on order number:

I am using this code:

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();
    }

and calling it like this:

  gvOrderHistory.DataSource = gridDataSource;
                gvOrderHistory.DataBind();
                GroupGridView(gvOrderHistory.Rows, 0, 1);

I am following this link . Now I want that Color of alternate groups should be different ( not alternate rows). One group should be green. then next group white and then third group again green and then white and so on. How to do this ?

You can add a column to the gridview and give different numbers for different groups and then in RowDataBound event handler of the grid, you can change the color of row based on the column number.

You can set the color of row like:
e.Row.BackColor = System.Drawing.Color.LightBlue;

If Im reading your code right, couldn't you just reference your ctrl variable and use the attributes property? For example:

ctrl.Attributes.Add("class", "classname");

I wasn't sure what variable you're trying to apply this to, but this would work for your nextCell TableCell object as well..

HTH

first add a css class to your aspx form

<style>
    .green { background-color: #00ff21; }
</style>

then add the extra codes as shown below

        ....
   ==>  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)
            {

             ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM