gridview find selected id/key of selected row

Hi,
How to find the value of the id / primary key column of a given row in a gridview?  I wish to put the value into a session variable to be passed to another page.  This value is located in the gridview in the boudfield named num.

The data for this gridview is supplied from other code behind.  This code is not shown below.  The data already displays in the gridview with no problems.

I have supplied the code that I am using.
Thanks, LLJ45
aspx code:
<asp:GridView ID="grdResults" runat="server" AutoGenerateColumns="false"
                            HorizontalAlign="Center" BorderWidth="5" CellPadding="4" >
                            <Columns>
                                <asp:BoundField DataField="Name" HeaderText="Name" />
                                <asp:BoundField DataField="Certification Date" HeaderText="Certification Date" />
                                <asp:BoundField DataField="Expiration Date" HeaderText="Expiration Date" />
                                <asp:TemplateField HeaderText="Select For Credit">
                                    <ItemTemplate>
                                        <asp:Button ID="btnSelect" Runat="server" Text="" CommandName="Select">
                                        </asp:Button>
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="num" visible="false" />
                            </Columns>
                        </asp:GridView>

aspx.vb code:
Protected Sub grdResults_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdResults.RowCommand
        If e.CommandName = "Select" Then
            Response.Redirect("~/credit_test_results2.aspx", True)
        End If
End Sub

Protected Sub grdResults_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdResults.RowDataBound

        If e.Row.RowType <> DataControlRowType.Header And e.Row.RowType <> DataControlRowType.Footer Then
            Dim btn As Button = CType(e.Row.Cells(0).FindControl("btnSelect"), Button)
            btn.Text = "Select For Credit " & Web.UI.DataBinder.Eval(e.Row.DataItem, "Name")
            btn.OnClientClick = "javascript:return confirm('Click OK, To Credit\n\n" & Web.UI.DataBinder.Eval(e.Row.DataItem, "Name") & " \n\nOne Test!');"

End Sub

Protected Sub grdResults_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdResults.SelectedIndexChanged
' this event never seems to fire?
End Sub

Open in new window

llj45Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rajeeshmcaCommented:
Hi llj45,

u can use the datakeys property of the gridview

assign the datakeynames property of the gridview to the primarykey field like

datakeynames="CustomerID"

in your code behind u can access the id using

Protected Sub grd_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdResults.SelectedIndexChanged
        Dim index As Integer = CustomersGridView.SelectedIndex
        Message.Text = "The primary key value of the selected row is " &    
        grd.DataKeys(index).Value.ToString()
End Sub
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
llj45Author Commented:
Hi, rajeeshmca
  Thanks for the help!
  I continue to find that the SelectedIndexChanged event of the gridview is NOT firing.  I have done some research.  I have this code at the top of my aspx page.  Also, I have set a breakpoint in the code behind on the SelectedIndexChanged event.

<%@ Page EnableViewState="true" Language="VB" AutoEventWireup="true" CodeFile="credit_test_results.aspx.vb" Inherits="esc_inspector_credit_test_results"
    enableEventValidation="false" %>

Also, I have set AutoGenerateSelectButton="true" on the gridview to create the Select button which should fire the SelectedIndexChanged event when the Select button is clicked?

Finally, I use this code to set the DataKeyNames property and populate the gridview.
If dsInsp.Tables(0).Rows.Count > 0 Then
                    grdResults.DataKeyNames = New String() {"num"}
                    grdResults.DataSource = dsInsp.Tables(0)
                    grdResults.DataBind()
End If
where num is the "primary key field" for the data in the gridview
OK?

LLJ45
0
rajeeshmcaCommented:
Is the SelectedIndexChanged firing or not??
 if no, try deleting the event handler and add a new event handler and check it out..
0
llj45Author Commented:
Hi, rajeeshmca
  Thanks for the help!
  Your code worked and answered my question.
Thanks, Lloyd

PS
In .NET 3.5 don't ever use a button like this one:
<asp:Button ID="btnSelect" Runat="server" Text="" CommandName="Select"></asp:Button>
on a gridview with a template field and with the gridview's own select button.
  The CommandName ="Select" will cause the SelectedIndexChanged event not to fire on the gridview!  If this command name is changed to something else, then the SelectedIndexChanged event fires.  Obivously, the command name of the <asp:Button> conflicts with the gridview's "select" button's code.
0
llj45Author Commented:
Thanks!  :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.