Solved trying to hide a GridView column

Posted on 2012-08-14
Last Modified: 2012-08-16
I am using the code below to populate an GridView with a data table
But when I run the following line I get an error:
                    Me.GridView1.Columns(1).Visible = False
It says that the index is out of range even though the GridView has several columns  

    Protected Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

            If Me.VATType.Text = "" Then
                Me.lblAddError.Text = "Please select a VAT type"
                Exit Sub
            End If

            If Me.Cat1.Text = "" Then
                Me.lblAddError.Text = "Please select Category1"
                Exit Sub
            End If
            If Me.Cat2.Text = "" Then
                Me.lblAddError.Text = "Please select Category2 (If none Select Category1 first)"
                Exit Sub
            End If
            If Me.Cat3.Text = "" Then
                Me.lblAddError.Text = "Please select Category3 (If none Select Category2 first)"
                Exit Sub
            End If

            Dim i As Integer
            Dim C1, C2, C3, C4, C5, C6, C7, C8, C9 As String

            dTable.Columns.Add("Gross Amount", GetType(String))
            dTable.Columns.Add("Credit", GetType(String))
            dTable.Columns.Add("VT", GetType(String))
            dTable.Columns.Add("VAT Amount", GetType(String))
            dTable.Columns.Add("Credit VAT", GetType(String))
            dTable.Columns.Add("exVAT", GetType(String))
            dTable.Columns.Add("Category Type 1", GetType(String))
            dTable.Columns.Add("Category Type 2", GetType(String))
            dTable.Columns.Add("Category Type 3", GetType(String))

            For i = 0 To Me.GridView1.Rows.Count - 1
                C1 = Me.GridView1.Rows(i).Cells(1).Text
                C2 = Me.GridView1.Rows(i).Cells(2).Text
                C3 = Me.GridView1.Rows(i).Cells(3).Text
                C4 = Me.GridView1.Rows(i).Cells(4).Text
                C5 = Me.GridView1.Rows(i).Cells(5).Text
                C6 = Me.GridView1.Rows(i).Cells(6).Text
                C7 = Me.GridView1.Rows(i).Cells(7).Text
                C8 = Me.GridView1.Rows(i).Cells(8).Text
                C9 = Me.GridView1.Rows(i).Cells(9).Text

                AddLine(dTable, C1, C2, C3, C4, C5, C6, C7, C8, C9)

            If IsNumeric(Me.GrossAmount_TextBox.Text) = False Then
                'this will take care of a blank space too
                Me.GrossAmount_TextBox.Text = "0"
            End If

            If IsNumeric(txtCredit1.Text) = False Then
                'this will take care of a blank space too
                Me.txtCredit1.Text = "0"
            End If
            If IsNumeric(VATAmount_TextBox.Text) = False Then
                'this will take care of a blank space too
                Me.VATAmount_TextBox.Text = "0"
            End If
            If IsNumeric(txtCredit2.Text) = False Then
                'this will take care of a blank space too
                Me.txtCredit2.Text = "0"
            End If

            If IsNumeric(NonVAT_Amount_TextBox.Text) = False Then
                'this will take care of a blank space too
                Me.NonVAT_Amount_TextBox.Text = "0"
            End If

            AddLine(dTable, GrossAmount_TextBox.Text, txtCredit1.Text, VATType.Text, VATAmount_TextBox.Text, txtCredit2.Text, NonVAT_Amount_TextBox.Text, Cat1.Text, Cat2.Text, Cat3.Text)
            ''HERE YOU CAN CALL AddLine as many times as you want with different parameters
            GridView1.DataSource = dTable

            GridView1.Rows(0).Cells(1).Width = 98
            GridView1.Rows(0).Cells(2).Width = 62
            GridView1.Rows(0).Cells(3).Width = 38
            GridView1.Rows(0).Cells(4).Width = 85
            GridView1.Rows(0).Cells(5).Width = 52
            GridView1.Rows(0).Cells(6).Width = 52
            GridView1.Rows(0).Cells(7).Width = 175
            GridView1.Rows(0).Cells(8).Width = 170
            GridView1.Rows(0).Cells(9).Width = 170

        Catch ex As Exception
            Me.lblAddError.Text = ex.Message
        End Try

    End Sub

    Sub AddLine(ByRef dt As DataTable, ByVal GrossAmt As String, ByVal credit As String, ByVal vatType As String, ByVal vatAmt As String, ByVal credvat As String, ByVal exVatAmt As String, ByVal cat1 As String, ByVal cat2 As String, ByVal cat3 As String)

        Dim dr As DataRow
        dr = dt.NewRow()
        dr("Gross Amount") = GrossAmt
        dr("Credit") = credit
        dr("VT") = vatType
        dr("VAT Amount") = vatAmt
        dr("Credit VAT") = credvat
        dr("exVAT") = exVatAmt
        dr("Category Type 1") = cat1
        dr("Category Type 2") = cat2
        dr("Category Type 3") = cat3

    End Sub
Question by:murbro
    LVL 1

    Expert Comment

    I think that loading the data at run time like this, you can access the Row or Column values like you're trying to do at the top of your post.

    Assuming you're creating/defining the GridView at design time (in the aspx), you should be able to just set the property of that column to not visible.

    In this example:
    <asp:GridView ID="GV_RecList" runat="server" CssClass="GV_General" Width="90%" 
    	GridLines="None" AutoGenerateColumns="False" CellPadding="4">
    		<asp:BoundField DataField="recId" HeaderText="Rec ID" ReadOnly="True" Visible="False">
    		<asp:BoundField DataField="lastName" HeaderText="Last Name" SortExpression="lastName" >
    			<ItemStyle Width="25%" />
    		<asp:BoundField DataField="firstName" HeaderText="First Name" SortExpression="firstName" >
    			<ItemStyle Width="25%" />
    		<asp:BoundField DataField="jobTitle" HeaderText="Job Title" SortExpression="jobTitle" >
    			<ItemStyle Width="10%" />
    		<asp:BoundField DataField="dept" HeaderText="Department" SortExpression="dept" >
    			<ItemStyle Width="18%" />
    	<PagerSettings Mode="NumericFirstLast" />
    	<RowStyle CssClass="GV_Row" />
    	<AlternatingRowStyle CssClass="GV_AltRow" />
    	<HeaderStyle CssClass="GV_Header" />
    	<FooterStyle CssClass="GV_Footer" />
    	<PagerStyle CssClass="GV_Pager" />

    Open in new window

    In the example, see the properties for the RecID column, which Visible is set to false.
    LVL 13

    Accepted Solution

    Try this

    protected void gvDocuments_RowDataBound(object sender, GridViewRowEventArgs e)
                e.Row.Cells[1].Visible = false;


    protected void gvDocuments_RowDataBound(object sender, GridViewRowEventArgs e)
                this.DataGridView1.Columns["ID coloumn"].Visible = false;

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    This video discusses moving either the default database or any database to a new volume.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now