ASP.net GridView column out of index

Hi

In ASP.net I am using the following code to make my last column invisible
 GridView2.Columns(7).Visible = False
but am getting an error that it is out of range
The GridView has five template columns and binds to a table with three columns.
(As a test to see what GridView2.Columns(2).Visible = False does - it deletes the third
template column)
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Paul MacDonaldDirector, Information SystemsCommented:
Are you hiding the column before or after you bind the grid?
0
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
after

        If Not IsPostBack Then

            ' Declare the query string.

            Dim queryString2 As String = _
  "Select * From Questions"

            ' Run the query and bind the resulting DataSet
            ' to the GridView control.
            Dim ds2 As DataSet = GetData(queryString2)
            If (ds2.Tables.Count > 0) Then

                Me.GridView2.DataSource = ds2
                Me.GridView2.DataBind()



            Else

                'Me.Label_Error1.Text = "Unable to connect to the database. " & Err.Description

            End If

            GridView2.Columns(7).Visible = False

        End If
0
Paul MacDonaldDirector, Information SystemsCommented:
Hmm.  And you're sure the column exists at the time you try to set its visibility?  I have a grid (not databound the way yours is) where I do essentially the same thing without any issue.

What does a GridView2.Columns.Count return just prior to that visible=false command?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
The column count is 5 instead of 8. Very strange
0
ddayx10Commented:
The GridView has five template columns and binds to a table with three columns.

The way this reads to me, you should only have 5 columns. Therefore anything outside of index 0 thru 4 is going to fail.

I understand that you have 8 columns otherwise you wouldn't be trying to reach that high, but on the face of it there's not enough info to see what's up...

Can you post your gridview so we can see how you are getting more columns than the 5 templatefields?
0
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi
Here is my GridView. I now have six columns. Thanks

<asp:GridView ID="GridView2" runat="server" BackColor="White"
                        BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4"
                        Height="116px" Width="795px">
                                    <Columns>
               
                              <asp:TemplateField HeaderText="Answer" HeaderStyle-Font-Size ="Small">
                  <ItemTemplate>
                    <asp:Label ID="LabelAnswer" runat="server"
                      CommandName="MyAnswer"
                      CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                      Text="" />
                  </ItemTemplate>
                      </asp:TemplateField>

                <asp:TemplateField HeaderText="Strongly Agree" HeaderStyle-Font-Size ="Small" ControlStyle-Font-Bold ="false" ControlStyle-Forecolor ="#006600" ControlStyle-Font-Size ="Smaller">
                  <ItemTemplate>
                    <asp:Button ID="Button1" runat="server"
                      CommandName="MyButton1"
                      CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                      Text="Strongly Agree" />
                  </ItemTemplate>

                </asp:TemplateField>
                <asp:TemplateField HeaderText="Agree" HeaderStyle-Font-Size ="Small" ControlStyle-Font-Bold ="false" ControlStyle-Forecolor ="#009900" ControlStyle-Font-Size ="Smaller">
                  <ItemTemplate>
                    <asp:Button ID="Button2" runat="server"
                      CommandName="MyButton2"
                      CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                      Text="Agree" />
                  </ItemTemplate>
                      </asp:TemplateField>

                                     <asp:TemplateField HeaderText="Undecided" HeaderStyle-Font-Size ="Small" ControlStyle-Font-Bold ="false" ControlStyle-Forecolor ="Gray" ControlStyle-Font-Size ="Smaller">
                  <ItemTemplate>
                    <asp:Button ID="Button3" runat="server"
                      CommandName="MyButton3"
                      CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                      Text="Undecided" />
                  </ItemTemplate>
                      </asp:TemplateField>

                                     <asp:TemplateField HeaderText="Disagree" HeaderStyle-Font-Size ="Small" ControlStyle-Font-Bold ="false" ControlStyle-Forecolor ="#FF6600" ControlStyle-Font-Size ="Smaller">
                  <ItemTemplate>
                    <asp:Button ID="Button4" runat="server"
                      CommandName="MyButton4"
                      CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                      Text="Disagree" />
                  </ItemTemplate>
                      </asp:TemplateField>

                                     <asp:TemplateField HeaderText="Strongly Disagree" HeaderStyle-Font-Size ="Small" ControlStyle-Font-Bold ="false" ControlStyle-Forecolor ="Red" ControlStyle-Font-Size ="Smaller">
                  <ItemTemplate>
                    <asp:Button ID="Button5" runat="server"
                      CommandName="MyButton5"
                      CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
                      Text="Strongly Disagree" />
                  </ItemTemplate>
                      </asp:TemplateField>
            </Columns>
                        <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                        <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
                        <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                        <RowStyle BackColor="White" ForeColor="#003399" />
                        <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                        <SortedAscendingCellStyle BackColor="#EDF6F6" />
                        <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                        <SortedDescendingCellStyle BackColor="#D6DFDF" />
                        <SortedDescendingHeaderStyle BackColor="#002876" />
                    </asp:GridView>
0
Paul MacDonaldDirector, Information SystemsCommented:
The other columns have to be bound columns.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
thanks for the help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.