Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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)
Avatar of Paul MacDonald
Paul MacDonald
Flag of United States of America image

Are you hiding the column before or after you bind the grid?
Avatar of Murray Brown

ASKER

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
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?
The column count is 5 instead of 8. Very strange
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?
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>
ASKER CERTIFIED SOLUTION
Avatar of Paul MacDonald
Paul MacDonald
Flag of United States of America 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
thanks for the help