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

    Adding a link to the existing column in the gridview

    Hello,

    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.

    CodeBehind:

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

    }
    }
    protected void FillGrid()
    {
    con.Open();
    SqlCommand cmd = new SqlCommand(sql, con); // sql will be coming getSql() method
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    int count = ds.Tables[0].Rows.Count;
    con.Close();
    if (ds.Tables[0].Rows.Count > 0)
    {
    grdTEW.DataSource = ds;
    grdTEW.DataBind();
    lblResults.Visible = true;
    lblResults.Text= "Results for Search on " + sTitle_Search + ".";
    lblMsg.Visible = false;
    }
    else
    {
    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.

    Thanks!!!!
  • #766478
    Hi,

    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">
    <columns>
    <asp:TemplateField HeaderText="Name" HeaderStyle-ForeColor="White">
    <ItemTemplate>
    <asp:Label ID="lblDesc" runat="server" />
    <asp:LinkButton ID="lnkVal" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Code") %>' ></asp:LinkButton>
    </ItemTemplate>
    </asp:TemplateField>
    </columns>
    </asp:GridView>


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

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

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #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;
    e.Row.Cells[ColumnIndex.Column1].Controls.Add(link);
    }
    }

    Thanks
    Koolprasd2003
    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.
    SRI RAMA PHANI BHUSHAN KAMBHAMPATI

  • #766919
    Hi,
    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;
    gvRow.Cells[0].Controls.Add(hLink);
    }

  • #766957
    Hi,

    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;
    gr.Cells[5].Controls.Add(hpq);
    }


  • Sign In to post your comments