?
Solved

gridview find selected id/key of selected row

Posted on 2010-04-06
5
Medium Priority
?
623 Views
Last Modified: 2012-05-09
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

0
Comment
Question by:llj45
  • 3
  • 2
5 Comments
 
LVL 15

Accepted Solution

by:
rajeeshmca earned 2000 total points
ID: 29992008
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
 

Author Comment

by:llj45
ID: 30025684
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
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 30086159
Is the SelectedIndexChanged firing or not??
 if no, try deleting the event handler and add a new event handler and check it out..
0
 

Author Comment

by:llj45
ID: 30129971
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
 

Author Closing Comment

by:llj45
ID: 31711418
Thanks!  :-)
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses
Course of the Month5 days, 18 hours left to enroll

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question