You must Sign In to post a response.
  • Category: ASP.NET

    Adding a link to the existing column in the gridview


    In my project, gridview is binded from dataset. In which one of the column must be having a half of it as text and other half as link with a relative URL. It should be like below:

    TEW 100 -----> "TEW" must be added programatically and 100 should be shown like link with relative URL .
    I can't add a Bound field or template field to gridview, as this is a search page containing 8 textboxes depending upon search sql statements from different tables in database , gridview will be displayed .

    By using below code , my output for that column is like 100 --->showing up like a text.


    protected void Page_Load(object sender, EventArgs e)
    if (IsPostBack)
    string finalsql = "";
    finalsql = getSql(); // Method to get a sql

    protected void FillGrid()
    SqlCommand cmd = new SqlCommand(sql, con); // sql will be coming getSql() method
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    int count = ds.Tables[0].Rows.Count;
    if (ds.Tables[0].Rows.Count > 0)
    grdTEW.DataSource = ds;
    lblResults.Visible = true;
    lblResults.Text= "Results for Search on " + sTitle_Search + ".";
    lblMsg.Visible = false;
    lblResults.Visible = true;
    lblResults.Text = "Results for Search on " + sTitle_Search + ".";
    lblMsg.Visible = true;
    lblMsg.Text = "No records found for this search, please try again.";
    grdTEW.DataSource = null;

    ASPX code:
    <asp:GridView ID="grdTEW" runat="server" > </asp:GridView>

    Need some suggestions.

  • #766478

    Use 2 different controls, one label and one linkbutton in same template, as you mentioned text you are assigned through code so assign label text property on rowdatabound event of gridview as you want.

    for your reference the design would be like below

    <asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" OnRowDataBound="gv_OnRowDataBound">
    <asp:TemplateField HeaderText="Name" HeaderStyle-ForeColor="White">
    <asp:Label ID="lblDesc" runat="server" />
    <asp:LinkButton ID="lnkVal" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Code") %>' ></asp:LinkButton>

    and onrowdatabound of gridview assign label text value as you want.

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #766479
    Thanks for the fast reply. If I use the above code it is generating a new column with link, but I need a link within the existing column, because gridview is filling with sql statements execution.

  • #766565
    you can check condition if you want to show dynamic link column in gridview, if yes then you can directly add link column to it
    see below snippet

    protected void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
    if (e.Row.RowType == DataControlRowType.DataRow)
    HyperLink link = new HyperLink();
    link.Text = "This is a link!";
    link.NavigateUrl = "Navigate somewhere based on data: " + e.Row.DataItem;

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766911
    You can add a dynamic hyper link column for a static grid in the row databound event and if you want a static hyperlink you can create Template column.

  • #766919
    After grdTEW.DataBind();, add this:

    foreach (GridViewRow gvRow in grdTEW.Rows)
    HyperLink hLink = new HyperLink();
    hLink.Text = gvRow.Cells[0].Text;
    hLink.NavigateUrl = "~/WebFormXYZ.aspx?linkText=" + hLink.Text;

  • #766957

    Adding a dynamic grid helped to solve the issue but I have a problem using that, some of the rows in that column does not data, but the link is coming to all the rows. Like "Quote File" is appearing to all rows in that column even though data is not there in that cell.

    How to overcome this issue?

    foreach (GridViewRow gr in grdTEW.Rows)
    HyperLink hpq = new HyperLink();
    hpq.Text = "Quote File" ;
    hpq.Target = "_blank";
    hpq.NavigateUrl = gr.Cells[5].Text;

  • Sign In to post your comments