0
votes

I have a grid view. Datatable is the data source.

I am looking for solution where I can add links to gridview column. But not by creating hyperlink field or not even want to play with designer part.

This is a common gridview, which will get dynamic data including columns.

Not necessary every datasource will have 'Name' column which is my target as hyper link.

I am able to generate <a href='...'>text</a> but it is displaying as is in gridview.

HTML block :

<asp:GridView ID="gvCommonDashboard" runat="server">  

</asp:GridView>

Code Behind :

foreach (DataRow dr in dt.Rows)
{                        
    string name = Convert.ToString(dr["Name"]);

    string url = SPContext.Current.Web.Url + listName + "/" + name;
     dr["Name"] = string.Format("<a href='" + url + "'>" + name + "</a>");

}
dt.AcceptChanges();                                     

gvCommonGrid.DataSource = dt;
gvCommonGrid.DataBind();
gvCommonGrid.Visible = true;
2
why you are not using hyperlink - Nazir Ullah
you mean hyperlink field? - Gaurravs
yea hyper link field or hyper link inside template field - Nazir Ullah
I dont want to add another column, just need to modify the value of existing column from data source to be in link format - Gaurravs
ok show your code html and code behide that what you are trying - Nazir Ullah

2 Answers

1
votes

You can build the link in RowDataBound Event of the gridview -

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string decodedText = HttpUtility.HtmlDecode(e.Row.Cells[CellIndex].Text);
        e.Row.Cells[CellIndex].Text = decodedText;
    }
}

Change the CellIndex to your actual cell index number.

0
votes

You can format the string using

string.Format("<a href='" + fullPath + "'>Text</a>");

i have tried this its works fine for me.