Search code examples
c#asp.netgridviewlinkbutton

how to create a link in gridview in asp.net


I am trying to create a webpage that has a gridview. this gridview is supposed to have a link like below

http://localhost/Test.aspx?code=123

when the user clicks one of the rows' link in gridview, it will open a blank page and display some result.

here is how I bind data to the gridview but I dont know how to set the link

protected void Page_Load(object sender, EventArgs e)
{
    string firma_no = logoFrmNr.ToString().PadLeft(3, '0');
    string active_period = logoFrmPeriod.PadLeft(2, '0');

    SqlConnection conn = new SqlConnection(conStr);
    string selectSql = @"SELECT 
                                LOGICALREF,
                                CODE , 
                                DEFINITION_ ,
                                FROM 
                                LG_CLFLINE";

    SqlCommand cmd = new SqlCommand(selectSql, conn);
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();

    conn.Close();
}

here is the markup

<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True">
</asp:GridView>

How can I make a link out of CODE column?


Solution

  • to me it would be something like

    <asp:DataGrid id="MyDataGrid" 
               GridLines="Both"
               AutoGenerateColumns="false"
               runat="server">
    
             <HeaderStyle BackColor="#aaaadd"/>
    
             <Columns>
    
                <asp:HyperLinkColumn
                     HeaderText="Select an Item"
                     DataNavigateUrlField="code"
                     DataNavigateUrlFormatString="http://localhost/Test.aspx?code={0}"
                     Target="_blank"/>
    
             </Columns>
    
          </asp:DataGrid>