I’m filling in a DataTable with all the necessary data. I want the “Process” column to hold a hyperlink so that it directs to a specific page when clicked (based on the Process name.
This is the code I have so far:
Object[] row = new Object[7];
HyperLink link = new HyperLink();
link.Text = Process.ToString();
link.NavigateUrl = String.Format("~/IndexSummary?Process={0}", Process.ToString());
row[0] = link;
//...Fill in rest of row
dt.LoadDataRow(row,false);
GridView1.DataSource = dt;
GridView1.DataBind();
When I execute this code, “System.Web.UI.WebControls.HyperLink” shows up for every row. This is my GridView code:
<asp:GridView ID="GridView1" runat="server" CellPadding="3"
ForeColor="#333333" GridLines="None"
AllowSorting="True" SortedAscendingHeaderStyle-CssClass="sortasc-header"
SortedDescendingHeaderStyle-CssClass="sortdesc-header" AutoGenerateSelectButton="True"
AllowPaging="True" PageSize="17" PagerSettings-Mode="NextPreviousFirstLast"
ShowHeaderWhenEmpty="True" PagerStyle-Font-Names="WebDings" PagerStyle-Font-Size="Medium"
PagerSettings-FirstPageText=" 7 " PagerSettings-PreviousPageText=" 3 "
PagerSettings-NextPageText=" 4 " PagerSettings-LastPageText=" 8 "
Font-Size="Small" onselectedindexchanged="GridView1_SelectedIndexChanged"
onpageindexchanging="GridView1_PageIndexChanging"
onsorting="GridView1_Sorting">
<AlternatingRowStyle BackColor="White" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerSettings FirstPageText=" 7 " LastPageText=" 8 " Mode="NextPreviousFirstLast" NextPageText=" 4 " PreviousPageText=" 3 "></PagerSettings>
<PagerStyle BackColor="#5D7B9D" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#DCE2E8" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingHeaderStyle CssClass="sortasc-header"></SortedAscendingHeaderStyle>
<SortedDescendingHeaderStyle CssClass="sortdesc-header"></SortedDescendingHeaderStyle>
</asp:GridView>
How can I change this to show the hyperlink correctly?
Just coming back to answer my question in case it can help someone else out:
I had to do the hyperlink adding in the RowDataBound event like this: