First off: I am aware that there have been some vaguely similar requests on here already, but none give a complete solution to my problem, and I’m at my wit’s end trying to piece together a solution.
Anyway, so I am writing a web interface to get some info from a RESTful API. I am generating a datatable, which I display via a gridview like this:
DataTable table = new DataTable();
table.Columns.Add("entry name");
table.Columns.Add("col1");
table.Columns.Add("col2");
table.Columns.Add("etc");
foreach(string name in GetNames())
{
DataRow row = table.NewRow();
row["name"] = name;
row["col1"] = SomeMethod(name);
row["col2"] = SomeOtherMethod(name);
row["etc"] = YouGetTheIdea(name);
table.Rows.Add(row);
}
GridView1.DataSource = table;
GridView1.DataBind();
So far so fine. What I would really like is for the “name” column to be hyperlinks, such that either they call a method with the name as a parameter; or link to a website, with the name as part of the URL, i.e. like this:
"<a href:'www.example.com/" + name + ".csv'>" + name + "</a>"
I can’t for the life of me figure out how to do these things as dynamically as I would like. Your help would be much appreciated. Thanks.
One solution is to have your column value be the URL and then for a bound column, set the
HtmlEncode = false. This would mean, in yourforeachyou’d do something likerow['link'] = "<a href='..'>..</a>". When this is bound, it should output as HTML.Alternatively, you can hook into the OnRowDataBound event on the gridview and manipulate the value in your code behind.
Or, finally, you can create an ItemTemplate column that binds appropriately: