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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
KinjalKumar PatelSenior 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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_samiCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.