Select buttons in datagrid

  • Thread starter Thread starter Vik
  • Start date Start date
V

Vik

If there are a few Select buttons in a datagrid, is there a way to
distinguish in code which button was clicked?

Thanks.
 
Do you mean that there is a select button for each row, or do you have
multiple select buttons for each row? If the latter, then why do you have
multiple select buttons for one row? If the former then you can determine
which select button was clicked by the SelectedIndex value, e.g.

'This is the method that handles the SelectedIndexChanged event of the
datagrid
Private Sub DataGrid1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles DataGrid1.SelectedIndexChanged
Dim iRowIndex As Integer = CType(sender, DataGrid).SelectedIndex
End Sub
 
I want to have multiple select buttons in each row to perform different
actions on the row.

Vik
 
You can use the CommandName parameter of the ButtonColumn control to be able
to determine the type of action that the user wish to perform on the record,
e.g.:

<asp:DataGrid ID="DataGrid1" Runat="server">
<Columns>
<asp:ButtonColumn DataTextField="Field1" CommandName="Select"
ButtonType="PushButton"></asp:ButtonColumn>
<asp:ButtonColumn Text="Delete Row" CommandName="Delete"
ButtonType="PushButton"></asp:ButtonColumn>
<asp:ButtonColumn Text="Duplicate Row" CommandName="Duplicate"
ButtonType="PushButton"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>

And in handling the ItemCommand event of the datagrid, you would write code
similar to this:

private void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
{
switch (e.CommandName.ToString ())
{
case "Select":
//selection code
break;
case "Duplicate":
//do Duplicate code
break;
case "Delete":
//delete code
break;
}

}
 
Thanks.

Vik

Phillip Williams said:
You can use the CommandName parameter of the ButtonColumn control to be
able
to determine the type of action that the user wish to perform on the
record,
e.g.:

<asp:DataGrid ID="DataGrid1" Runat="server">
<Columns>
<asp:ButtonColumn DataTextField="Field1" CommandName="Select"
ButtonType="PushButton"></asp:ButtonColumn>
<asp:ButtonColumn Text="Delete Row" CommandName="Delete"
ButtonType="PushButton"></asp:ButtonColumn>
<asp:ButtonColumn Text="Duplicate Row" CommandName="Duplicate"
ButtonType="PushButton"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>

And in handling the ItemCommand event of the datagrid, you would write
code
similar to this:

private void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs
e)
{
switch (e.CommandName.ToString ())
{
case "Select":
//selection code
break;
case "Duplicate":
//do Duplicate code
break;
case "Delete":
//delete code
break;
}

}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
 
each button is associated with an id, examine that id to determine what
button fired.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top