views:

9

answers:

1

I have a grid view will two different button columns. I want to perform a different action depending on what button the user presses. How in the SelectedIndexChanged event do I determine what colmun was pressed. This is the code I use to generate the columns.

            grdAttachments.Columns.Clear();
            ButtonField bfSelect = new ButtonField();
            bfSelect.HeaderText = "View";
            bfSelect.ButtonType = ButtonType.Link;
            bfSelect.CommandName = "Select";
            bfSelect.Text = "View";

            ButtonField bfLink = new ButtonField();
            bfLink.HeaderText = "Link/Unlink";
            bfLink.ButtonType = ButtonType.Link;
            bfLink.CommandName = "Select";
            bfLink.Text = "Link";

            grdAttachments.Columns.Add(bfSelect);
            grdAttachments.Columns.Add(bfLink);
+1  A: 

I think it would help if you give the buttons different CommandName properties.

Here is an MSDN example of reading CommandName in the GridView_RowCommand event, which specifically mentions your multiple-button situation:

  void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {

    // If multiple ButtonField column fields are used, use the
    // CommandName property to determine which button was clicked.
    if(e.CommandName=="Select")
    {

      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);    

      // Get the last name of the selected author from the appropriate
      // cell in the GridView control.
      GridViewRow selectedRow = CustomersGridView.Rows[index];
      TableCell contactName = selectedRow.Cells[1];
      string contact = contactName.Text;  

      // Display the selected author.
      Message.Text = "You selected " + contact + ".";

    }

  }
DOK
Thanks, I found that answer as well after doing more searching.Bob
Bob Avallone