I'm working with an ASP.net 2.0 GridView control that is bound to the results of a sql query, so it looks something like this:
<asp:GridView ID="MySitesGridView" runat="server" AutoGenerateColumns="False" DataSourceID="InventoryDB" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="GridView1_RowCommand" OnRowDataBound="siteRowDataBound">
<Columns>
<asp:BoundField DataField="Server" HeaderText="Server"/>
<asp:BoundField DataField="Customer" HeaderText="Customer" SortExpression="Customer" />
<asp:BoundField DataField="PublicIP" HeaderText="Site Address" DataFormatString="<a href='http://{0}/foo'>Go To Site</a>" />
</Columns>
</asp:GridView>
As you can see, I'm displaying links with addresses in one of the columns (the one bound to the PublicIP field) using the format string:
<a href='http://{0}/foo'>Go To Site</a>
Here's the problem: I need to use one of the other columns from the result set as well as the PublicIP column in my links, but I don't know how to make that available to my format string. I essentially need that column bound to two columns from the result set. To clarify, I need something like:
<a href='http://{0}/{1}'>Go To Site</a>
Where {1} is the value of my other column. Is there any way to accomplish this cleanly (even if it doesn't use format strings)? I've looked into using TemplateFields as well, but can see no easy way to do it with them either.
Best Solution
TemplateFields are the way to go.
I usually prefer to have a private string function in the Page which I pass several object variables, and calculate the resulting string.
and in the code-behind:
Advantage is that the function can be shared in a common parent class, or as a static public function of a utility class.