abennett10
asked on
Hyperlinks in Gridview c#
Hello all,
I have a gridview control in shich I have binded the datatable in code.
Is there a way for the data from my datatable be displayed as a
hyperlink in the gridview ?
Thanks!
I have a gridview control in shich I have binded the datatable in code.
Is there a way for the data from my datatable be displayed as a
hyperlink in the gridview ?
Thanks!
Or you can have templated field:
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Bind("YourFieldName")>'
Text=""></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Bind("YourFieldName")>'
Text=""></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry it takes me so long to respond....
but @KinjalPatel
So I just insert this in my gridview in my html view correct ?
but @KinjalPatel
So I just insert this in my gridview in my html view correct ?
ASKER
Guys,
I'm doing a late-binding....I'm not using the wizard to fill the Gridview, I am getting my own datatable and binding to the control in code, so The Gridview would not know a "Fieldname", How can I set this TemplateField up in code ?
I'm doing a late-binding....I'm not using the wizard to fill the Gridview, I am getting my own datatable and binding to the control in code, so The Gridview would not know a "Fieldname", How can I set this TemplateField up in code ?
but you do know the columnName you want to assign in advance right?
---> The Gridview would not know a "Fieldname", How can I set this TemplateField up in code ?
Or can you explain what problem you faced using above suggestions?
---> The Gridview would not know a "Fieldname", How can I set this TemplateField up in code ?
Or can you explain what problem you faced using above suggestions?
ASKER
True, I do know the columnName in advance, the problem is that the above suggestions create an extra column in the gridview and I want the existing fields to be hyperlinks.
-->True, I do know the columnName in advance,
then we need to see some code how you are creating GV and databinding.
then we need to see some code how you are creating GV and databinding.
ASKER
Ok, here ya go :
I created a method that creates a datatable in code from a result set from a database....
( statically creating my columns in code, looping through the result set and and filling in the datatable)
using that datatable I just did this below
gvHrsWrk.DataSource = GetHoursWorkedDT;
gvHrsWrk.DataBind();
since I created the datatable and columns in code, I know the column names.
I created a method that creates a datatable in code from a result set from a database....
( statically creating my columns in code, looping through the result set and and filling in the datatable)
using that datatable I just did this below
gvHrsWrk.DataSource = GetHoursWorkedDT;
gvHrsWrk.DataBind();
since I created the datatable and columns in code, I know the column names.
--->since I created the datatable and columns in code, I know the column names.
So now since you know the columnname what happens when you try the above given suggestions?
and/or share the gvHrsWrk markup as well showing what you tried.
So now since you know the columnname what happens when you try the above given suggestions?
and/or share the gvHrsWrk markup as well showing what you tried.
ASKER
I get this error :
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'LineID'
below is the markup:
<asp:GridView ID="gvHrsWrk" runat="server" CellPadding="4" ForeColor="Black" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Bold="True" AutoGenerateColumns="False ">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("LineID", "FieldInformation.aspx?Fie ldName={0} &type="+Re quest.Quer yString["t ype"]) %>'
Text='<%# Eval("LineID", "[{0}]") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
</asp:GridView>
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'LineID'
below is the markup:
<asp:GridView ID="gvHrsWrk" runat="server" CellPadding="4" ForeColor="Black" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Bold="True" AutoGenerateColumns="False
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("LineID", "FieldInformation.aspx?Fie
Text='<%# Eval("LineID", "[{0}]") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
</asp:GridView>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I apologize, looks like the Column name was "Line". I made that mistake, but it still created an extra column. see the image below......
The "Line" Column is duplicated
example.JPG
The "Line" Column is duplicated
example.JPG
ASKER
I'm such a goof!!!! I left Autogeneratecolumns = "true" should be false
ASKER
Thank you, You guys were very helpful and patient.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield.aspx
http://authors.aspalliance.com/aspxtreme/webforms/controls/addinghyperlinkfieldstogridview.aspx