Reputation: 979
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 column 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);
Upvotes: 1
Views: 8389
Reputation: 629
string commandName = e.CommandName.ToString().Trim();
GridViewRow row = GridView1.Rows[Convert.ToInt32(e.CommandArgument)];
switch (commandName)
{
case "showName":
LClickName.Text = "You Clicked Show Name Button : \"" + row.Cells[1].Text + "\"";
break;
case "EditName":
LClickName.Text = "You Clicked Edit Name Button : \"" + row.Cells[1].Text + "\"";
break;
default: break;
}
Here is a sample for Multiple select Button in One Gridview
Multiple Select Button In One Gridview
Upvotes: 0
Reputation: 32841
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 + ".";
}
}
Upvotes: 2