Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

row index in gridview rowCommand

Tags:

asp.net

Just want to transfer value from variable to nother variable :

 protected void gvVariableDetail_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "Edit")
            {

                 int index = Convert.ToInt32(e.CommandArgument);
                 GridViewRow gvRow = gvVariableConfig.Rows[index];
                 int rowIndex = index;

            }

        }

But rowindex still have zero value, and index got row value(in these case i try edit row 2 and so index value is 1(start by 0)). so hope anyone know how to transfer index value to rowIndex.

like image 401
Zen Iskan Avatar asked Nov 08 '12 09:11

Zen Iskan


1 Answers

You could use the CommandSource property and cast it's NamingContainer to the GridViewRow. Then you can use it's RowIndex property:

GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).NamingContainer; 
int rowIndex    = gvr.RowIndex;

If you want to use the CommandArgument you have to set it from aspx:

CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' 

then this also works:

int RowIndex = int.Parse(e.CommandArgument.ToString()); 
like image 175
Tim Schmelter Avatar answered Oct 08 '22 07:10

Tim Schmelter