views:

813

answers:

2

Hi,

I have a web application that I am working on(ASP.NET 2.0 C#). In it I have a GridView whose data source is an Oracle database. I get the data into the gridview in my codebehind, and don't set the datasource directly.

I wanted to create a hyperlink field (NAME) that takes me to a details page about a specific record. What ends up happening is that it creates the Hyperlink field as well as the regular field that it gets from the datasource, which I don't want. If I remove the field from my SELECT statement, it gives an error saying something like: "NAME" not found in datasource.

How can I eliminate the regular field, and get a hyperlink field instead? I have tried Gridview.Columns.Remove(columnlocation), but that won't work coz the columns don't exist there originally.

Please Help Thank you.

+3  A: 

Instead of removing columns, disable AutoGenerateColumns property of the gridview and set your hyperlink column manually like that :

<asp:gridview id="GridView1" 
    autogeneratecolumns="false"
    runat="server">                
     <asp:HyperLinkField DataNavigateUrlFields="UserID" 
      DataNavigateUrlFormatString="UserDetails.aspx?id={0}"
            DataTextField="UserName" />
</asp:gridview>
Canavar
+1 for wording it the easy way :)
roman m
+1  A: 

Sounds like you have AutoGenerateColumns property set to TRUE on your grid. This means that a column is generated for EVERY column you return from your query.

If you want to have some custom columns, you should set AutoGenerateColumns="false" and add all the columns to the GirdView as asp:BoundField and your Hyperlink column as asp:TemplateField

Let me know if I'm off the mark with this

here's some code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="Name" />
        <asp:BoundField DataField="Whatever" />
        <asp:TemplateField>
        <ItemTemplate>
        <a href='<%# Eval("UserId", "URL_TO_USER?userId={0}") %>'>Details</a>
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
roman m