KafMigadito
KafMigadito

Reputation: 28

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

Upvotes: 0

Views: 21186

Answers (4)

Davide Lettieri
Davide Lettieri

Reputation: 326

You can try to find the last column in the PreRender event

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>

Upvotes: 2

Sain Pradeep
Sain Pradeep

Reputation: 3125

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

Upvotes: 1

Damith
Damith

Reputation: 63065

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

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460098

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:

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:

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

Upvotes: 0

Related Questions