locked
Gridview:Get item name in row when a button in the same row is clicked RRS feed

  • Question

  • User641142935 posted

    Hi,

    I have a gridview with four colums:

    From, scrap, Date&time, Reply.

    Ex: 

        From                 Scrap                        Date & Time           

    SWG\dkurian   xxxxxxxxxxxx   4/16/2010 1:02:25 PM     <reply>

    <REPLY>SWG\hraja      xxxxxxxxxxxx    4/16/2010 1:02:25 PM     <reply>

    <REPLY>SWG\ashok     xxxxxxxxxxxx    4/16/2010 1:02:25 PM     <reply>

    <REPLY>SWG\anoop    xxxxxxxxxxxx    4/16/2010 1:02:25 PM      <reply>

     

    Note that <reply>  is a button in a template field. I would like to know how is it possible to catch the From value of the row when reply button is clicked from the same row in the grid. I want to pass this from value to a modal dialog which i have already created. How is that possible. Please help.

     

    Thanks,

    dkurian<REPLY>

    Saturday, April 17, 2010 5:03 PM

Answers

  • User626880745 posted

    all/any postback causing control within a row will raise the RowCommand event of the GridView

    protected void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
      {
        // If multiple buttons are used in a GridView control, use the
        // CommandName property to determine which button was clicked.
        if(e.CommandName=="Reply")
        {
          1.// Convert the row index stored in the CommandArgument
            // property to an Integer.
            //int index = Convert.ToInt32(e.CommandArgument);

          2.// Retrieve the row that contains the button clicked
            // by the user from the Rows collection.
            //GridViewRow row = ContactsGridView.Rows[index];
        
         1 & 2 above are one way of getting to the current row (refer to the example in the link for RowCommand

         you could also do this:
         GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;

        and once you have the row you can use FindControl to get to a TemplateField:
        Label lbl = (Label)row.FindControl("Label1");
       
       or .Cells[index] to get to a BoundField:
        string someValue = row.Cells[n].Text;       //where n starts at 0
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 17, 2010 10:30 PM