Hiding columns in a gridview

Camillia
Camillia used Ask the Experts™
on
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"?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
It still works with autogenerate. I think the problem is with when the event is called.

Comment out the command and check the column count at that point. Just to see if columns exist when the event is called.
let me try
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>
   
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Commented:
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.
Commented:
To hide a CustomerID column that is automatically generated during data binding, place the following code example in a DataBindingComplete event handler

Me.dataGridView1.Columns("CustomerID").Visible = False

Commented:
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.
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.
Commented:
The DataGridView (System.Windows.Forms) has the event DataBindingComplete, but unfortunately is not a web control.

But as mentioned in my previous comment, from what I am aware the GridView (System.Web.UI.WebControls) does not have the DataBindingComplete event handle.

You might have to try another event. Have you tried the onload method/ load event?
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.
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial