如何改变GridView最后一行的颜色
本文关键字:一行 颜色 GridView 何改变 改变 最后 | 更新日期: 2023-09-27 18:04:47
我在ASP中创建了一个网页。NET和c#。我取了一个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)
{
}
}
是的,使用GridView1.Rows[GridView1.Rows.Count - 1]
,您将获得gridview的最后一行,正如已经在@Soner Gönül回答中提到的。
但不是使用RowDataBound
事件,你需要使用PreRender
,这里我添加了一个类,通过CSS你可以做更多的样式的东西。
你也可以使用BackColor属性和设置任何十六进制颜色代码,并使其突出显示
代码:
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:添加!重要的你可以覆盖默认的样式
.highlighted td
{
color:Red ! important;
background-color: blue ! important;
}
使用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>
你可以找到你的最后一行使用它的Rows
属性
gridView1.Rows[gridView1.Rows.Count - 1]
,你可以设置BackColor
, ForeColor
或BorderColor
属性为;
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.
}
}