?
Solved

ASP.net GridView column out of index

Posted on 2011-10-19
8
Medium Priority
?
196 Views
Last Modified: 2012-05-12
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)
0
Comment
Question by:Murray Brown
  • 4
  • 3
8 Comments
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 36994534
Are you hiding the column before or after you bind the grid?
0
 

Author Comment

by:Murray Brown
ID: 36994706
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
 
LVL 34

Expert Comment

by:Paul MacDonald
ID: 36995434
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Murray Brown
ID: 36996268
The column count is 5 instead of 8. Very strange
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 36996550
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
 

Author Comment

by:Murray Brown
ID: 37010654
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
 
LVL 34

Accepted Solution

by:
Paul MacDonald earned 2000 total points
ID: 37013146
The other columns have to be bound columns.
0
 

Author Closing Comment

by:Murray Brown
ID: 37013181
thanks for the help
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month17 days, 1 hour left to enroll

862 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