如何在RowDataBound上的gridview中找到最后一行

时间:2013-09-13 09:15:42

标签: c# asp.net

 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,欢呼声

4 个答案:

答案 0 :(得分:2)

您可以尝试查找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>

答案 1 :(得分:1)

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

您可以确定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
         }
    }
}

答案 2 :(得分:0)

由于您评论了RowDataBound,您似乎想要检测//ROW FOOTER TOTAL中的页脚行,您只需要检查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

答案 3 :(得分:0)

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

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