Solved

Help with making column in GridView invisible

Posted on 2011-09-23
5
288 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank You!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 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

12 Experts available now in Live!

Get 1:1 Help Now