10

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.

3
  • That's cool you want to do that, but what is your question? Commented Nov 8, 2012 at 9:17
  • need to get row index for use with javascript, but i want to trigger those script by button, not by one of command in grid. so i want to transfer index value to global variable, to use by another button Commented Nov 8, 2012 at 9:22
  • A perfectly good response that still contains no questions. Commented Nov 8, 2012 at 9:27

1 Answer 1

18

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()); 
Sign up to request clarification or add additional context in comments.

3 Comments

got message Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'System.Web.UI.WebControls.GridViewRow'.
using approach 1 sir. i am using control in extended grid view to trigger RowCommand
i am newbie to asp so i will just post the code... protected void gvVariableDetail_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Edit") { ........... } } using last solution can't do it because convert string to int

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.