hiding a column

G

Guest

Hi
Where is the proper place to hide a column dynamically? I tried putting
this line in ItemDataBound,

myDataGrid.Columns[2].Visible = false;

but is giving me index out of range error! Am I putting this in a wrong
place (ItemDataBound)?

Any idea?
TIA

--
 
G

Guest

Well, I fixed the problem. We can not have AutoGenerateColumns="true", if
you want to set Column Visible property in ItemDataBound / Page_Load !!?

Can we have the AutoGenerateColumns="true", and still be able to set Column
Visible property elsewhere?
Anyone knows?
 
K

Kim Quigley

I tried setting AutoGenerateColumns to true and then changing column
visibility in the code behind but the column count is always 0. I can think
of 3 ways to get around this:

1-Don't use AutoGenerateColumns
2-Modify the select statement so that only certain columns are returned, if
binding to a database.
3-Create all of the columns programmatically.

Is there a reason that you have to use AutoGenerateColumns?

Well, I fixed the problem. We can not have AutoGenerateColumns="true", if
you want to set Column Visible property in ItemDataBound / Page_Load !!?

Can we have the AutoGenerateColumns="true", and still be able to set
Column
Visible property elsewhere?
Anyone knows?

Hi
Where is the proper place to hide a column dynamically? I tried putting
this line in ItemDataBound,

myDataGrid.Columns[2].Visible = false;

but is giving me index out of range error! Am I putting this in a wrong
place (ItemDataBound)?

Any idea?
TIA
 
S

Saladin

try this (from datagridgirl.com)

Question: How do I hide a column in my Datagrid if AutoGenerateColumns is
set to True?

Answer: AutoGenerated columns do not appear in the Datagrid's Columns()
collection, and so the usual method of hiding a Datagrid column will fail:

'Will NOT work for AutoGenerated columns:
Datagrid1.Columns(1).Visible = False

So the place to handle this is in the ItemDataBound event of the Datagrid:

<asp:DataGrid id="Datagrid1" runat="server" AutoGenerateColumns="True"
OnItemDataBound="Datagrid1_OnItemDataBound"/>

Private Sub DataGrid1_ItemDataBound(s As Object, e As DatagridItemEventArgs)
e.Item.Cells(1).Visible = False
End Sub

Graeme
 
E

Eliyahu Goldin

Yes, you can have AutoGenerateColumns="true" and set columns properties in
ItemCreated event.

Eliyahu

Well, I fixed the problem. We can not have AutoGenerateColumns="true", if
you want to set Column Visible property in ItemDataBound / Page_Load !!?

Can we have the AutoGenerateColumns="true", and still be able to set Column
Visible property elsewhere?
Anyone knows?

Hi
Where is the proper place to hide a column dynamically? I tried putting
this line in ItemDataBound,

myDataGrid.Columns[2].Visible = false;

but is giving me index out of range error! Am I putting this in a wrong
place (ItemDataBound)?

Any idea?
TIA
 

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,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top