I have aq gridview with Autogeneratecolumns="True".
I want to hide some columns on databound so I have this:
Sub gvOnDataItemBound(ByVal sender As Object, ByVal e As System.EventArgs)
GridView1.Columns.Item(2).Visible = False
End Sub
This gives me the error: "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index "
I see the columns. This is my gridview:
<asp:GridView AllowSorting="true" OnDataBound="gvOnDataItemBound" AllowPaging="true" HeaderStyle-BackColor="#284775" HeaderStyle-ForeColor="white" AlternatingRowStyle-BackColor="#E6E6FA" AutoGenerateEditButton="True" AutoGenerateColumns="true" DataSourceID="SqlDataSource1" ID="GridView1" runat="server">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" SelectCommandType="StoredProcedure" runat="server" UpdateCommand=" Update med set eye=@eye, MD=@MD, VisionPreInjection=@VisionPreInjection, VisionLastInjection = @VisionLastInjection where Number = @Number" SelectCommand="GetAllData" ConnectionString="<%$ ConnectionStrings:Medical %>" >
</asp:SqlDataSource>
theplonk
I'm not totally certain about ASP.net, but maybe the event is being called twice. Once when the control is declared/initiated, then once when the data is bound.
Use an alert or create a counter for when the event is opened, to see how many times it is opened.
Even try running your hide command in a different event like OnLoad.
Anilgr. It's a GridView part of the System.Web.UI.WebControls. Not a DataGridView of the System.Windows.Forms namespace.
From what i'm aware it does not have the DataBindingComplete event.
Camillia
ASKER
in a "DataBindingComplete" event handler??
I'll try it. I have another code that I make the column style change, in code behind, using onDataBound event handler. Dont think i've seen DataBinding Complete. Let me look and post back.