gridview hide column from the code behind

newbie27
newbie27 used Ask the Experts™
on
hello epxerts
i am trying to add a hyperlink column to the gridivew and hide the default custid column which is pullying the database

 <asp:gridview ID="grdCustomers" runat="server">
     <Columns>
            <asp:TemplateField HeaderText="Customer ID" InsertVisible="False" SortExpression="CustID">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("CustID") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "UpdateCustomer.aspx?CID=" & Eval("CustID") %>'
                        Text='<%# Eval("CustID") %>'></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
              <asp:BoundField DataField="CustID" HeaderText=""
                SortExpression="CustID" />
            </Columns>
           
    </asp:gridview>

i am geting this error

error BC30311: Value of type 'System.Web.UI.WebControls.BoundColumn' cannot be converted to 'System.Web.UI.WebControls.DataControlField'.

>> grdCustomers.Columns.Add(objbc)

please can you adivse?

thanks
Dim dbconn As SqlConnection
        Dim dbcomm As SqlCommand
        Dim ds As DataSet
        Dim myAdap As SqlDataAdapter


        dbconn = New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("CString").ConnectionString)
        dbconn.Open()
         

        dbcomm = New SqlCommand("GetCustomerDetails", dbconn)

        ds = New DataSet()
        myAdap.Fill(ds, "TempTable")

        Dim i As Integer
        For i = 0 To ds.Tables(0).Columns.Count - 1

            Dim objbc As New BoundColumn()
            objbc.DataField = ds.Tables(0).Columns(i).ColumnName
            objbc.HeaderText = ds.Tables(0).Columns(i).ColumnName

            If objbc.DataField = ds.Tables(0).Columns("CustId").ColumnName Then
                objbc.Visible = False
            End If

            grdCustomers.Columns.Add(objbc)
            grdCustomers.DataSource = ds.Tables(0)
            grdCustomers.DataBind()
        Next
        ' grdCustomers.DataSource = dbcomm.ExecuteReader()
        'grdCustomers.DataBind()

       

        dbconn.Close()

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial