GridView Multiple Select Buttons

G

Guest

Good morning!

How do I determine which SELECT button was clicked in a GridView?

The multiple SELECT buttons will be used for an application approval process.

Thank you in advance for your support in this matter.
 
G

Guest

Thank you for the alternative method in establishing an approval process.

However, could you provide me with an idea on how to accomplish determining
which SELECT button was clicked within a GridView.

Again, thanks!
 
J

Jan Hyde

Terry <[email protected]>'s wild thoughts were
released on Mon, 13 Mar 2006 08:15:27 -0800 bearing the
following fruit:
Good morning!

How do I determine which SELECT button was clicked in a GridView?

The multiple SELECT buttons will be used for an application approval process.

Thank you in advance for your support in this matter.

Use SelectedRow

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
GridView1.SelectedIndexChanged
' Get the currently selected row using the SelectedRow
property.
Dim row As GridViewRow = GridView1.SelectedRow
Page.Title = "You selected " & row.Cells(1).Text
End Sub

Jan Hyde (VB MVP)
 
G

Guest

Phillip, currently I am using several SELECT buttons in my GridView. As a
result, I am trying to determine which SELECT button within the Gridview was
clicked.

Again, could you provide a VB sample code for this solution.

Thank you for your assistance.
 
G

Guest

When you say "several SELECT buttons", do you mean one Select button per
GridView row or more than one Select button per row?

If the former then the previous MSDN link I posted applies.

But, if you mean more than one Select button per GridView row, then you are
not using this command properly. There should be only one Select button per
GridView row. Consider the ButtonField; each ButtonField performs a specific
command based on its CommandName property.
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.buttonfield(VS.80).aspx
 
Joined
Oct 14, 2006
Messages
1
Reaction score
0
How to Determnine which Button is clicked in a GridView in VB.NET

I found the solution:
I was facing a similar situation and this is what I did.
I added one SELECT button and one ButtonField which I called "btntrack".
When the select command was clicked I could trigger whatever event I wanted to. then I create the statement below to find out when the ButtonField was clicked and trigger a different event.
I also made the ButtonField appear and look like a select field by changing the ButtonType to Image and assigning a small image that would look good.
Then I created some code to make it act like a select command by changing the SelectedIndex every timea a different row was clicked.
"How to Determnine which Button is clicked in a GridView"
Protected Sub gvMain_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvMain.RowCommand
If e.CommandName = "btnTrack" Then
gvMain.SelectedIndex = e.CommandArgument
End If
End Sub

www.data2bi.com
Specialists in Business Intelligence implementation and development.
Database Desing, Management, Custom application development, Database perfomance tunning, Network Administration and VoIP Implementation.
 
Joined
Aug 2, 2010
Messages
1
Reaction score
0
I know this is an old post, but I figured there might be someone looking to do the same thing several years later like myself. Anyway, here is how i solved it:


I utilized one gridview to display two similar datasets. The exception was a few different columns that I tacked on the end of the gridview dynamically depending on which option was chosen.

This is code from one of the button click events. I'm dynamically creating an additional column and assigning it a custom CommandName. In this case, one button is Comments and the other is ViewAnswers.
Code:
ButtonField btnFldC = new ButtonField(); 
btnFldC.HeaderText = "Comments";
btnFldC.ImageUrl = "~/images/notes.png";
[COLOR="#0066FF"][B]btnFldC.CommandName = "Comments";[/B][/COLOR]
btnFldC.ButtonType = ButtonType.Image;
btnFldC.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
QLGridView.Columns.Insert(QLGridView.Columns.Count, btnFldC);

ButtonField btnFld = new ButtonField(); 
btnFld.HeaderText = "View Answers";
btnFld.ImageUrl = "~/images/mag1.png";
[COLOR="#0066FF"][B]btnFld.CommandName = "ViewAnswers";[/B][/COLOR]
btnFld.ButtonType = ButtonType.Image;
btnFld.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
QLGridView.Columns.Insert(QLGridView.Columns.Count, btnFld);


Now, in the RowCommand GridView Event, I set up a switch to determine which CommandName was selected. The crucial part that makes it work is the CommandArgument property which gives you the index of the row in which the button was clicked. It's of type object so you just need to cast it to an integer to use in the row subscript.
Code:
protected void QLGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
     GridViewRow row = [COLOR="#0066FF"][B]QLGridView.Rows[Convert.ToInt32(e.CommandArgument)];[/B][/COLOR]
     switch ([COLOR="#0066FF"][B]e.CommandName.Trim()[/B][/COLOR])
     {
          case "ViewAnswers":
                  SetAnswers(row.Cells[0].Text.ToString().Trim(), row.Cells[1].Text.ToString().Trim(),
                                   row.Cells[2].Text.ToString().Trim(), row.Cells[3].Text.ToString().Trim(),
                                   row.Cells[4].Text.ToString().Trim(), row.Cells[5].Text.ToString().Trim());
                  SetBlockOut(true);
                  break;
          case "SendFollowUp":
                  SetManagerCCPanel(true);
                  SetBlockOut(true);
                  break;
          case "Comments":
                  DataTable dt = DataManager.GetComments(row.Cells[0].Text.ToString().Trim(), row.Cells[1].Text.ToString().Trim(),
                                                               row.Cells[2].Text.ToString().Trim(), row.Cells[3].Text.ToString().Trim(),
                                                               row.Cells[4].Text.ToString().Trim(), row.Cells[5].Text.ToString().Trim());
                        if (dt != null && dt.Rows.Count > 0)                        
                            SetCommentsPanel(true, dt.Rows[0].ItemArray[0].ToString().Trim());                         
                        else
                            SetCommentsPanel(true, String.Empty);
                        SetBlockOut(true);
                        break;
           default:
                  break;
     }                
}

As with everything, there's probably a better way, but this works perfectly for me and is able to handle multiple buttons.

Thanks,
Josh
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top