简体   繁体   English

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

[英]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.

相关问题 如何在RowDataBound事件内有条件地绑定gridview行? - How can I conditionally bind a gridview row inside the RowDataBound event? 如何在DevExpress网格视图中找到组的最后一行? - How can I find the last row of a group in a DevExpress gridview? 如何在没有RowDataBound的GridView ItemTemplate中找到DropDownList? - How to find a DropDownList in a GridView ItemTemplate without RowDataBound? 我在gridview的RowDataBound中找不到行,给出了异常“索引超出范围。 必须为非负数,并且小于集合的大小。” - I can't find row in gridview's RowDataBound, gives exception“Index was out of range. Must be non-negative and less than the size of the collection.” GridView RowDataBound 处理程序 - 无法从行获取数据 - GridView RowDataBound Handler - Can't get data from row 在Gridview RowDatabound期间找不到单元格的超链接 - Can't Find Hyperlink of Cell during Gridview RowDatabound 为什么rowdatabound在gridview中找不到链接按钮? - Why rowdatabound can't find a link button in gridview? 如何从Gridview-RowDataBound返回值以在链接中使用它。 C# - How can i return value from Gridview-RowDataBound to use it in link. C# 动态gridview-如何在rowdatabound中引用 - Dynamic gridview - how to reference in rowdatabound 无法在RowDataBound上设置GridView行宽 - Unable to set GridView row widths on RowDataBound
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM