如何更改GridView最后一行的颜色

时间:2015-07-26 11:15:26

标签: c# asp.net gridview

我使用c#在ASP.NET中创建了一个网页。我带了一个GridView。 我想更改或突出显示GridView的最后一行的颜色。

请参阅以下代码: -

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



     }   
}

3 个答案:

答案 0 :(得分:2)

GridView1.Rows[GridView1.Rows.Count - 1],您将获得gridview的最后一行,如@SonerGönül回答中所述。

但是,不是使用RowDataBound事件,而是需要使用PreRender,我在这里添加了一个类,通过CSS可以做更多样式化的事情

代码背后:

  protected void GridView1_PreRender(object sender, EventArgs e)
    {
        GridViewRow getRow = GridView1.Rows[GridView1.Rows.Count - 1];
        getRow.Attributes.Add("class", "highlighted");
    }

Css:添加!重要的,您可以覆盖默认样式

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

<强> Blog Article : Gridview related Tutorials

答案 1 :(得分:1)

只需使用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>

答案 2 :(得分:1)

您可以使用Rows property作为

找到最后一行
gridView1.Rows[gridView1.Rows.Count - 1] 

您可以将其设置为BackColorForeColorBorderColor属性;

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.
    }   
}