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!
abennett10Asked:
Who is Participating?
 
KinjalKumar PatelConnect With a Mentor Senior Software EngineerCommented:
You can implement it in the following way.
<asp:TemplateField>
                    <ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("FieldName", "FieldInformation.aspx?FieldName={0}&type="+Request.QueryString["type"]) %>'
                            Text='<%# Eval("FieldName", "[{0}]")
%>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

Above example also explains you that how you can tweak the URL by adding some query string values to it. Hope it makes more clear to you.
0
 
guru_samiCommented:
Or you can have templated field:
 <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Bind("YourFieldName")>'
                            Text=""></asp:HyperLink>
                    </ItemTemplate>
</asp:TemplateField>
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
abennett10Author Commented:
Sorry it takes me so long to respond....

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

0
 
guru_samiCommented:
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?
0
 
abennett10Author Commented:
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.
0
 
guru_samiCommented:
-->True, I do know the columnName in advance,
then we need to see some code how you are creating GV and databinding.
0
 
abennett10Author Commented:
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.
0
 
guru_samiCommented:
--->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.
0
 
abennett10Author Commented:
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>
0
 
guru_samiConnect With a Mentor Commented:
Your datatable should have a column with name  - "LineID"
0
 
abennett10Author Commented:
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
0
 
abennett10Author Commented:
I'm such a goof!!!! I left Autogeneratecolumns = "true" should be false
0
 
abennett10Author Commented:
Thank you, You guys were very helpful and patient.

0
All Courses

From novice to tech pro — start learning today.