Solved

Help with making column in GridView invisible

Posted on 2011-09-23
5
308 Views
Last Modified: 2012-06-27
Hello,

When I use the code below to set column(1) of my GridView to invisible, I receive the following error:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Code:

Dim ds As New DataSet
        ds.ReadXml(Server.MapPath("~/App_Data/Country.xml"))
        GridView1.DataSource = ds.Tables(0)
        GridView1.DataBind()
        GridView1.Columns(1).Visible = False


How do I fix this problem?

Thanks,

0
Comment
Question by:vcharles
  • 3
5 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 36588651
You should do this on the RowCreated event like this:

Protected Sub GridView1_RowCreated(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
            e.Row.Cells(1).Attributes.Add("style", "display:none")
    End Sub
0
 
LVL 12

Expert Comment

by:Ramkisan Jagtap
ID: 36588731
plz check with the colunms u have in data source.
U can check it with
Gridview1.Columns.Count
Count might have only one.
0
 

Author Comment

by:vcharles
ID: 36589861
Hi,
The code works but I need to achieve this by code. I have one grid displaying data from several xml files, the column I need to hide will depend on which xml file is being displayed.

Thanks,

Victor
0
 

Author Comment

by:vcharles
ID: 36589900
Never mind my last post, I will add if statements for different xml files being loaded in the code to hide different columns.

Thanks,

Victor
0
 

Author Closing Comment

by:vcharles
ID: 36589906
Thank You!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now