Link to home
Start Free TrialLog in
Avatar of abennett10
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!
Avatar of guru_sami
guru_sami
Flag of United States of America image

Or you can have templated field:
 <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Bind("YourFieldName")>'
                            Text=""></asp:HyperLink>
                    </ItemTemplate>
</asp:TemplateField>
ASKER CERTIFIED SOLUTION
Avatar of KinjalKumar Patel
KinjalKumar Patel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of abennett10
abennett10

ASKER

Sorry it takes me so long to respond....

but @KinjalPatel
So I just insert this in my gridview in my html view correct ?
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 ?

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?
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.
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.
--->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.
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?FieldName={0}&type="+Request.QueryString["type"]) %>'
                            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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
I'm such a goof!!!! I left Autogeneratecolumns = "true" should be false
Thank you, You guys were very helpful and patient.