How to hide columns in Datagrid

J

J Gao

Hi All,
I am using Data binding with DataGrid. I need to hide column in the grid
using
DataGrid1.Columns(1).Visible = False

But I get the following error message

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

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

Source Error:

Line 119:
Line 120: Private Sub Button1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button1.Click
Line 121: DataGrid1.Columns(1).Visible = False
Line 122: End Sub
Line 123:


It seems that Datagrid does not recognize the columns created at runtime.

Any idea how to do this?

Thanks in advance.



Jie
 
J

J Gao

I got it. Use
Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemCreated

Select Case e.Item.ItemType

Case ListItemType.Header

e.Item.Cells(1).Visible = False

e.Item.Cells(2).Visible = False




End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top