Murray Brown
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).Visib le = 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).Visib le = False does - it deletes the third
template column)
In ASP.net I am using the following code to make my last column invisible
GridView2.Columns(7).Visib
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).Visib
template column)
Are you hiding the column before or after you bind the grid?
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).Visib le = False
End If
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).Visib
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?
What does a GridView2.Columns.Count return just prior to that visible=false command?
ASKER
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?
ASKER
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,GridViewRo w).RowInde x %>"
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,GridViewRo w).RowInde x %>"
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,GridViewRo w).RowInde x %>"
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,GridViewRo w).RowInde x %>"
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,GridViewRo w).RowInde x %>"
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,GridViewRo w).RowInde x %>"
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" />
<SortedAscendingHeaderStyl e BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderSty le BackColor="#002876" />
</asp:GridView>
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,GridViewRo
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,GridViewRo
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,GridViewRo
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,GridViewRo
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,GridViewRo
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,GridViewRo
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" />
<SortedAscendingHeaderStyl
<SortedDescendingCellStyle
<SortedDescendingHeaderSty
</asp:GridView>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks for the help