[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

GridView: Programmatically adding columns to the control (HyperLink)

Posted on 2011-04-22
7
Medium Priority
?
938 Views
Last Modified: 2012-05-11
Hi,

Minor problem here. I am trying to find a way to add programmatically columns to a GridView control. Most of the code I have read on the internet is unclear here.

Suppose I have a datatable filled with the following ids:
23424323
45645646
13123212

And I need to render a hyperlink as part of the gridview, where there is a column of hyperlinks like "edit.aspx?id=23424323", preferably with just the word "edit" showing.

Thanks,
Ryan
0
Comment
Question by:rossryan
  • 3
  • 2
  • 2
7 Comments
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35449311
Hi,
Please use following
Thansk
<asp:GridView ID="grd" runat="server">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:HyperLink ID="hpl" runat="server" Text="Edit" NavigateUrl='edit.aspx?id=<%# Eval("DBField") %>'></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

Open in new window

0
 

Author Comment

by:rossryan
ID: 35449318
Programmatically.
0
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35449387
sorry there is typo use it as

<asp:HyperLink ID="hpl" runat="server" Text="Edit" NavigateUrl='<%# "edit.aspx?id=" + Eval("DBField") %>'></asp:HyperLink>
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.

 

Author Comment

by:rossryan
ID: 35449425
Hmm. Perhaps I should explain it like this:

I am looking for any code that let's me dynamically create those columns. What I mean is, I'm slotting this into a web control, and I do not know beforehand just how many columns will be hyperlink columns. The number of columns will be determined by the value I receive when the control is created.

Thanks,
Ryan
0
 
LVL 16

Assisted Solution

by:Imran Javed Zia
Imran Javed Zia earned 800 total points
ID: 35449453
Please follow thses links: and add Hyperlink instead of TextBox
http://www.codeproject.com/KB/aspnet/create_template_columns.aspx
0
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 1200 total points
ID: 35449656
Hello, do you want to do something simple like this?

Paste this in a user control file ascx:
<%@ Import Namespace="System.Data" %>
<%@ Control Language="C#" ClassName="MyWebUserControl" %>
<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public void InitData(DataTable dtt)
    {
        foreach (DataColumn col in dtt.Columns)
        {
            HyperLinkField linkColumn = new HyperLinkField();
            linkColumn.HeaderText = col.ColumnName;
            linkColumn.DataTextField = col.ColumnName;
            linkColumn.DataNavigateUrlFields = new string[] { col.ColumnName };
            linkColumn.DataNavigateUrlFormatString = "~/MyPage.aspx?Param1={0}";
            GridView1.Columns.Add(linkColumn);
        }
        
        GridView1.DataSource = dtt;
        GridView1.DataBind();
    }
    
</script>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
</asp:GridView>

Open in new window


Then in your aspx page add this control and you can do this:
MyWebUserControl1.InitData(myDataTable);

Open in new window


May be I'm over simplifying your problem.
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35449679
I want to show you that you can add columns to your GridView in a easy way. This must be done before call GridView.DataBind() method.

HyperLinkField linkColumn = new HyperLinkField();
linkColumn.HeaderText = "Column Title";
GridView1.Columns.Add(linkColumn);

Open in new window

0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question