[英]How can i find Last row in a gridview on RowDataBound
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
**if (e.Row.RowIndex >= gridview1.PageSize) // ROW FOOTER TOTAL**
{
e.Row.BackColor = System.Drawing.Color.Firebrick;
e.Row.ForeColor = System.Drawing.Color.White;
}
}
This code works sometimes, someone can help me 该代码有时有效,有人可以帮助我
DM,cheers DM,欢呼声
You can try to find the last column in the PreRender event 您可以尝试在PreRender事件中找到最后一列
protected void grid_PreRender(object sender, EventArgs e)
{
GridViewRow row = grdAlert.Rows[grdAlert.Rows.Count - 1];
// do stuff with your row
}
If you just need to change the style of the footer you can use 如果只需要更改页脚的样式,则可以使用
<asp:GridView ID="grid" runat="server" FooterStyle="your style"></asp:GridView>
A grid view doesn't appear to have a row count until it's finished binding each row. 网格视图在完成每行的绑定之前似乎没有行数。 So, another thought:
因此,另一个想法是:
Can you determine the number of rows from the datatable that the gridview is binding to, then store that in a variable for use later? 您能否从网格视图绑定到的数据表中确定行数,然后将其存储在变量中以备后用?
you can find last row like this 你可以找到像这样的最后一行
GridViewRow row = GridView1.Rows[GridView1.Rows.Count-1];
or use this 或使用这个
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView grid = (GridView)sender;
if(e.Row.RowIndex == (grid.Rows.Count - 1))
{
//last row
}
}
}
It seems that you want to detect the footer row in RowDataBound
since you have commented //ROW FOOTER TOTAL
, you just have to check for the DataControlRowType.Footer
: 似乎您要在
RowDataBound
检测页脚行,因为您已经注释了//ROW FOOTER TOTAL
,只需检查DataControlRowType.Footer
:
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
// here it is
}
}
Otherwise you could compare the RowIndex
with the row of the undrlying DataItem
: 否则,您可以将
RowIndex
与展开的DataItem
的行进行比较:
DataRowView row = (DataRowView)e.Row.DataItem;
if (e.Row.RowIndex == row.DataView.Count - 1) ; // last row
In your page load method set colors as below 在页面加载方法中,设置颜色如下
protected void Page_Load(object sender, EventArgs e)
{
//code what you currently have ....
// add below code after that
GridViewRow row = GridView1.Rows.Count-1;
row.BackColor = System.Drawing.Color.Firebrick;
row.ForeColor = System.Drawing.Color.White;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.