如何在 RowDataBound 上的网格视图中找到最后一行

本文关键字:最后 一行 RowDataBound 视图 网格 | 更新日期: 2023-09-27 18:31:20

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

这段代码有时有效,有人可以帮助我

DM,欢呼

如何在 RowDataBound 上的网格视图中找到最后一行

您可以尝试在 PreRender 事件中查找最后一列

protected void grid_PreRender(object sender, EventArgs e)
{
    GridViewRow row = grdAlert.Rows[grdAlert.Rows.Count - 1];
    // do stuff with your row
}

如果您只需要更改页脚的样式,则可以使用

<asp:GridView ID="grid" runat="server" FooterStyle="your style"></asp:GridView>
网格

视图在完成每行绑定之前似乎没有行计数。所以,另一个想法:

是否可以确定 gridview 绑定到的数据表中的行数,然后将其存储在变量中以供以后使用?

你可以像这样找到最后一行

GridViewRow row = GridView1.Rows[GridView1.Rows.Count-1];

或使用此

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

既然您已经评论了//ROW FOOTER TOTAL,您似乎想在RowDataBound中检测页脚行,您只需要检查DataControlRowType.Footer

protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        // here it is
    }
}

否则,您可以将RowIndex与未处理DataItem行进行比较:

DataRowView row = (DataRowView)e.Row.DataItem;
if (e.Row.RowIndex == row.DataView.Count - 1) ; // last row

在页面加载方法中,设置颜色如下

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