Mudassir Hasan
Mudassir Hasan

Reputation: 28771

get row index from link button in footer template

I am frustated with this problem with Gridview.

I want to get row index in RowCommand event of gridview when linkbutton present in footer row is clicked.

I always get -1 as value in index

        Control ctl = e.CommandSource as Control;
        GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;
        int index = CurrentRow.RowIndex;

How to get out of this problem ?

Upvotes: 3

Views: 1061

Answers (1)

Cosmin
Cosmin

Reputation: 24

from what I know the footer row will have always the RowIndex = -1. I've tried to do some tests, you can try it also, and for each row of DataRow type, we'll have an RowIndex, but for the row of FooterRow type, we'll have always -1 value. Below it's my test code(it's for information purposes only, some lines of code are not needed).

protected void grdGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Control ctl = e.CommandSource as Control;
            GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;
            int index;

            foreach (GridViewRow row in grdGrid.Rows)
            {
                if (CurrentRow.RowType == DataControlRowType.DataRow)
                {
                    //you'll have an RowIndex for each DataRow: 0,1,2 and so on
                    index = CurrentRow.RowIndex;
                }
                else if (CurrentRow.RowType == DataControlRowType.Footer)
                    {
                        //the RowIndex will be always -1 
                        index = CurrentRow.RowIndex;
                    }

            }
        }

I hope this will help you.

Upvotes: 1

Related Questions