how to hide auto generate columns in datagrid?

H

Hanson

I have a datagrid control in an aspx page. The datagrid columns will be
dynamically generated according to the pre-page's search condition, I want
to hide some columns in the datagrid. is that anyway to do that?
 
R

Richard

Set in the datagrid,

AutoGenerateColumns="false"

<asp:datagrid runat="server" autogeneratecolumns="false">
.....boundcolumns stuff....
</asp:datagrid>

Autogeneratecolumns, Indicates whether the DataGrid should automatically
generate BoundColumn columns using the data supplied in a data source (the
DataGrid will use all fields in the data source to do so).
 
R

Richard

Sorry Hanson, i misunderstood the question a bit,
anyway,

use a Visible = False

to make it invisible and not be showed then
 
H

Hanson

Thanks first for your response.

But you might misunderstand my question:

1), The datagrid's column is generated in runtime not in design time, because the columns is depone the pre-page's search condition. only in runtime, when I construct the dataTable, I can know how many columns in the datagrid.
2) I can not access the auto-generated columns (when I debug, it should the datagrid.columns.count=0 ), then I can not set the column's visible value;


Any other help is very appreciated!


Hi,

You could use DataGridColumn's Visible property.

Cosmin
 
C

Cosmin Marin

Hi,

As Richard also suggested (and if my understanding is correct this time): set AutoGenerateColumns to false and dynamically add your columns based on what you have in the datatable.

Cosmin
Thanks first for your response.

But you might misunderstand my question:

1), The datagrid's column is generated in runtime not in design time, because the columns is depone the pre-page's search condition. only in runtime, when I construct the dataTable, I can know how many columns in the datagrid.
2) I can not access the auto-generated columns (when I debug, it should the datagrid.columns.count=0 ), then I can not set the column's visible value;


Any other help is very appreciated!


Hi,

You could use DataGridColumn's Visible property.

Cosmin
 
H

Hanson

Hi Richard,

Thanks for your reply first. However the problem is I can not access the autogenerated columns, and then I can not set the column's visible property.


Sorry Hanson, i misunderstood the question a bit,
anyway,

use a Visible = False

to make it invisible and not be showed then
 
R

Ryan S

You could try something like:

Dim dgcol As System.Web.UI.WebControls.DataGridColumn
For Each dgcol In myDG.Columns
If dgcol.HeaderText = "column to hide" Then dgcol.Visible = False
Next

in Page_Load after calling myDG.DataBind(). That will work with explicity
defined columns. I'm not sure if that works with auto-generated columns.

Good luck!

-Ryan

Hi Richard,

Thanks for your reply first. However the problem is I can not access the
autogenerated columns, and then I can not set the column's visible property.


Sorry Hanson, i misunderstood the question a bit,
anyway,

use a Visible = False

to make it invisible and not be showed then
 
P

Peter Högström

Hi,
found this on http://www.datagridgirl.com/faq.aspx

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





*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top