Avatar of Camillia
Camillia
Flag for United States of America asked on

Hiding columns in a gridview

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 "

Why ? is it because of Autogenerate="True"?
ASP.NETVisual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Camillia

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
theplonk

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Camillia

ASKER
let me try
Camillia

ASKER
it's zero! why??

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
SOLUTION
anilgr

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
theplonk

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.
SOLUTION
theplonk

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Camillia

ASKER
oh, sorry missed that you said that's for forms...

I think the ondataBound should work. I still think it's because i have "autogenerated="true".

I'll play around and google. If i find the answer, will post.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Camillia

ASKER
yeah, it was because i had autogenerate to true.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=274936&SiteID=1

I changed to autogenerate=false and used Bound columns. Now, onDataBound works.