Hiding Unbound DataGrid Columns

MESDM

New member
Joined
Mar 14, 2005
Messages
3
Programming Experience
3-5
Hello to everyone!

The names Max, and I'm a bit of a newby to VB.NET / ASP.NET.

My problem is this..

I have a datagrid on a web page, which is bound to its data at runtime. Everything is working fine - Sorting, Filtering etc, except hiding columns.

when I try to hide one of the columns, I receive the following error message:
  • Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
I know the index value being used, is not outside of the range as I've tried 0, 1 etc.. through to the number of columns.

The VB.NET code I'm using is below:
  • [Me.dgPubs.Columns(myIndex).Visible = False]
Also tried:
  • [Me.dgPubs.Columns.Items(myIndex).Visible = False]
I also counted the columns from vb code and the result was 1.

Any help would be appreciated.

Thanks in advance - Max
 
Hi Max. Welcome to the forums. I'd like to encourage you to post in the forum most appropriate to your question, and I'd like to discourage you from posting the same question in more than one forum.

Any array or collection in VB.NET has elements or items at indexes ranging from zero to (number of elements or items - 1), so you are possibly going one column too far. For example, if you have 5 columns then theirs indexes are 0, 1, 2, 3, 4 so if you are going to the number of columns then you're looking for a column at index 5, which does not exist.

The Columns property of the WebForms DataGrid has a Count property that is the number of columns. If you wanted to iterate over all the columns you would use code like:
VB.NET:
For i As Integer = 0 To myGrid.Columns.Count - 1 Step 1
	'Do something with myGrid.Columns(i).
Next i
 
I know there to be 5 col's, so I used a FOR loop inside a button click event to count/iterate through the number of col's..
The result was 1

Thanks all the same for a speedy and polite reply, I might try something different.
 
Last edited:
Back
Top