Jay Desai
Jay Desai

Reputation: 871

How to change the color of last row of GridView

I have created one webpage in ASP.NET with c#. I have taken one GridView. I want to change or highlight the color of last row of GridView.

Please Refer below code :-

protected void Page_Load(object sender, EventArgs e)
{
    DataSet ds = new DataSet();
    OracleConnection con = new OracleConnection("Data Source=10.31.41.103/ORCL;User ID=RL_PET;Password=RL_PET;Unicode=True");
    con.Open();
    OracleDataAdapter a = new OracleDataAdapter("SELECT NVL(MERGE,'GRAND_TOTAL') AS MERGE, COUNT(PALLET_NO) AS Total_Pallets , SUM(NET_WT) AS Net_Weight , SUM(GROSS_WT) AS Gross_Weight FROM WI_PALLET WHERE DATA_STS IN (2,3) AND TRANS_TYPE = 'P' GROUP BY ROLLUP (MERGE)", con);
    a.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
    GridView1.Visible = true;
    con.Close();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {



     }   
}

Upvotes: 0

Views: 4966

Answers (3)

Satinder singh
Satinder singh

Reputation: 10218

Yes with GridView1.Rows[GridView1.Rows.Count - 1] you will get the last row of gridview, as already mentioned in @Soner Gönül answer.

But instead of using the RowDataBound event, you need to use PreRender, here I added a class and by CSS you can do more styling stuff.

You can also use the BackColor property and set any hex color code and make it highlighted

Code Behind:

  protected void GridView1_PreRender(object sender, EventArgs e)
    {
        GridViewRow getRow = GridView1.Rows[GridView1.Rows.Count - 1];
        getRow.Attributes.Add("class", "highlighted");
        // or
         getRow.BackColor = System.Drawing.Color.FromName("#EE0000");
    }

CSS : By adding ! important you can override the default styling

 .highlighted td
  {
     color:Red ! important;
     background-color: blue ! important;
  }

Upvotes: 2

Robert McKee
Robert McKee

Reputation: 21492

Just use CSS:

tr:last-child { color: red; }

tr:last-child { color:red; }
<table>
  <tr>
    <td>Head1</td>
        <td>Head2</td>
        <td>Head3</td>
    </tr>
  <tr>
    <td>Col1</td>
        <td>Col1</td>
        <td>Col1</td>
    </tr>
    <tr>
    <td>Col1</td>
        <td>Col1</td>
        <td>Col1</td>
    </tr>
    <tr>
    <td>Col1</td>
        <td>Col1</td>
        <td>Col1</td>
    </tr>
    <tr>
    <td>Col1</td>
        <td>Col1</td>
        <td>Col1</td>
    </tr>
  </table>

Upvotes: 2

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98868

You can find your last row using it's Rows property as

gridView1.Rows[gridView1.Rows.Count - 1] 

and you can set it's BackColor, ForeColor or BorderColor properties as;

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        gridView1.Rows[gridView1.Rows.Count - 1].BackColor = Color.Yellow;
        // Or you can set which color you want in Color enumeration.
    }   
}

Upvotes: 1

Related Questions