locked
Gridview Row Color RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below code . I want when there is Sub Total in second column row there should be some background color . When there is Grand Total in first column then there should be some other background color

    public void gvw_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.Cells[1].Text == "Sub Total")
                    {
                        e.Row.Cells[0].Text = "";
                        e.Row.Attributes.Add("style", "this.style.backgroundColor = '#FFFFFF';");
                    }
                }
            }
    
    
    select CASE WHEN GROUPING(Location) = 1 AND GROUPING(EmployeeId) = 1 THEN 'Grand Total' else Location end as Location,
    	CASE WHEN GROUPING(EmployeeId) = 1 THEN 'Sub Total' else EmployeeId end as EmployeeId,
    	sum(Amount) as Amount 
    	FROM [Data] where Amount <> 0 
    	group by Location,EmployeeId with rollup

    Thanks

    Sunday, September 23, 2018 11:04 AM

Answers

  • User-893317190 posted

    Hi JagjitSingh,

    If you want to use e.Row.Attributes, you should know what it will be rendered as.

    In you code , it will be rendered as  

    <tr style="this.style.backgroundColor = '#FFFFFF';">
    			
    		</tr>

    This is not javascript .It's html attribute. So you should write your value of the attribute as the value of the html attribute style such as background-color:red;

    Below is my code.

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.Cells[1].Text == "Sub Total"&&e.Row.Cells[0].Text!="Grand Total")
                    {
                        e.Row.Cells[0].Text = "";
                        e.Row.Attributes.Add("style", "background-color:#F1234F");
                    }
                    if(e.Row.Cells[0].Text == "Grand Total")
                    {
                        e.Row.Attributes.Add("style", "background-color:#FFF11F");
                        e.Row.Cells[0].Text = "";
                        e.Row.Cells[1].Text = "Grand Total";
                    }
                }
    
            }

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 24, 2018 2:59 AM

All replies

  • User-1716253493 posted

    cell color

            if(e.Row.RowType==DataControlRowType.DataRow)
            {
                string location=string.Format("{0}",DataBinder.Eval(e.Row.DataItem,"location"));
                if(location=="Grand Total")
                {
                    e.Row.Cells[1].BackColor = System.Drawing.Color.Red;
                }
            }

    row color

    e.Row.BackColor = System.Drawing.Color.Red;

    Similar for sub total, change the color below grand total

    Monday, September 24, 2018 1:03 AM
  • User-893317190 posted

    Hi JagjitSingh,

    If you want to use e.Row.Attributes, you should know what it will be rendered as.

    In you code , it will be rendered as  

    <tr style="this.style.backgroundColor = '#FFFFFF';">
    			
    		</tr>

    This is not javascript .It's html attribute. So you should write your value of the attribute as the value of the html attribute style such as background-color:red;

    Below is my code.

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    if (e.Row.Cells[1].Text == "Sub Total"&&e.Row.Cells[0].Text!="Grand Total")
                    {
                        e.Row.Cells[0].Text = "";
                        e.Row.Attributes.Add("style", "background-color:#F1234F");
                    }
                    if(e.Row.Cells[0].Text == "Grand Total")
                    {
                        e.Row.Attributes.Add("style", "background-color:#FFF11F");
                        e.Row.Cells[0].Text = "";
                        e.Row.Cells[1].Text = "Grand Total";
                    }
                }
    
            }

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 24, 2018 2:59 AM