Code to display 'Select' column in an unbound GridView control

Discussion in 'C#' started by hcole, May 17, 2006.

  1. #1
    Hi people,

    Im working in Visual Web Developer, Using ASP.NET and VB.NET linking to SQL Server 2000. I have an unbound Gridview control on the form which displays the results of an SQL query. All working fine but I want the leftmost column to behave like a hyperlink so that when the user clicks on it, I can pick up the column data and load another page based on the information. The code I have so far is this:

    Code to generate the grid:

    
    <asp:GridView ID ="grdListMembers" 
    runat = "Server" 
    AllowSorting=true 
    OnSelectedIndexChanged="grdListMembers_SelectedIndexChanged" 
    style="z-index: 100; left: 0px; position: absolute; top: 0px"  
    OnSelectedIndexChanging="grdListMembers_SelectedIndexChanging" EnableSortingAndPagingCallbacks="True">
    <HeaderStyle ForeColor="DarkSlateBlue" />
    </asp:GridView>
    
    Code (markup):
    Code to populate the grid:
    
                    adpListMembers = New SqlClient.SqlDataAdapter(qryString, ListMembers)
                    Dim dsListMembers As New DataSet
                    adpListMembers.Fill(dsListMembers, "Members")
                    grdListMembers.DataSource = dsListMembers.Tables("Members").DefaultView
                    grdListMembers.DataBind()
    
    Code (markup):
    Any help would be greatly appreciated!
     
    hcole, May 17, 2006 IP
  2. jfilley

    jfilley Peon

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here is a hyperlink column, this is assuming the dataset that your grid is bound to contains a MemberID field. Just change that to whatever your field name is that is the parameter you are trying to pass.

    <Columns>
    <asp:HyperLinkColumn Text="View Member" DataNavigateUrlField="MemberID" DataNavigateUrlFormatString="/ViewMember.aspx?MemberID={0}"></asp:HyperLinkColumn>
    </Columns>
     
    jfilley, May 19, 2006 IP
  3. hcole

    hcole Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi jfilley.

    I have that working, only problem is, the SQL query I use for the unbound part of the grid includes the membername so membername is now appearing as a hyperlink field and a standard column. If i take membername out of the SQL statement the grid is blank. (Membername is the primary key for the table Im querying)
     
    hcole, May 22, 2006 IP