Link to home
Start Free TrialLog in
Avatar of rossryan
rossryan

asked on

GridView: Programmatically adding columns to the control (HyperLink)

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
Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

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

Avatar of rossryan
rossryan

ASKER

Programmatically.
sorry there is typo use it as

<asp:HyperLink ID="hpl" runat="server" Text="Edit" NavigateUrl='<%# "edit.aspx?id=" + Eval("DBField") %>'></asp:HyperLink>
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
SOLUTION
Avatar of Imran Javed Zia
Imran Javed Zia
Flag of Pakistan image

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
ASKER CERTIFIED 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 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