Hiding DataGrid Columns when AutoGenerateColumns="true"

J

Jerad Rose

I am working with a DataGrid (ASP.NET 2.0) that is set to
AutoGenerateColumns="true". It is being populated from a DataSet. There is
a column that I want to hide from the display, but I want to leave it in the
DataSet so that it can be referenced. However, all attempts that I've tried
are failing.

The main method I see being used is this:

myColumn.ColumnMapping = MappingType.Hidden

However, this isn't working -- the column remains visible.

If I do this:

myDataTable.Columns.Remove("MyColumn")

Then the column is hidden, but of course, this removes it from the dataset.

Of course, I can't hide the column after the DataGrid is bound, because the
Columns collection isn't populated when using AutoGenerateColumns="true"
(the reason for this still baffles me, but I've given up on figuring that
one out).

I'm aware that I could hide each individual cell on ItemDataBound, but this
requires referencing the cell through a numeric index, which is a poor
practice, IMO (what if the order of the columns changes?).

Here is the full code block:

Dim myDataSet As DataSet = GetDataSet()

myDataSet.Tables("Table").Columns("MyColumn").ColumnMapping =
MappingType.Hidden

DataGrid1.DataSource = myDataTable
DataGrid1.DataBind()

Is there not a way to hide a column for a DataGrid that is using
AutoGenerateColumns="true", by referencing the column by name?

Thanks in advance for your help.

Jerad
 
J

Jerad Rose

Thanks for the response.

Unfortunately, I don't see how I can use this event to hide a column. This
event is fired when creating row items, not columns (it passes
DataGridItemEventArgs, same as the ItemDataBound event). I still do not
seem to have access to any sort of collection or object representing the
column I want to hide.

Can you elaborate on how I can achieve this through this event?

Thanks again.

Jerad

Eliyahu Goldin said:
You can catch autocreated columns (an hide them) in ItemCreated event.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jerad Rose said:
I am working with a DataGrid (ASP.NET 2.0) that is set to
AutoGenerateColumns="true". It is being populated from a DataSet. There
is a column that I want to hide from the display, but I want to leave it
in the DataSet so that it can be referenced. However, all attempts that
I've tried are failing.

The main method I see being used is this:

myColumn.ColumnMapping = MappingType.Hidden

However, this isn't working -- the column remains visible.

If I do this:

myDataTable.Columns.Remove("MyColumn")

Then the column is hidden, but of course, this removes it from the
dataset.

Of course, I can't hide the column after the DataGrid is bound, because
the Columns collection isn't populated when using
AutoGenerateColumns="true" (the reason for this still baffles me, but
I've given up on figuring that one out).

I'm aware that I could hide each individual cell on ItemDataBound, but
this requires referencing the cell through a numeric index, which is a
poor practice, IMO (what if the order of the columns changes?).

Here is the full code block:

Dim myDataSet As DataSet = GetDataSet()

myDataSet.Tables("Table").Columns("MyColumn").ColumnMapping =
MappingType.Hidden

DataGrid1.DataSource = myDataTable
DataGrid1.DataBind()

Is there not a way to hide a column for a DataGrid that is using
AutoGenerateColumns="true", by referencing the column by name?

Thanks in advance for your help.

Jerad
 
E

Eliyahu Goldin

In this event you should be able to locate the cell belonging to the column
you want to hide. You can hide the column by hiding individual cells.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Jerad Rose said:
Thanks for the response.

Unfortunately, I don't see how I can use this event to hide a column. This
event is fired when creating row items, not columns (it passes
DataGridItemEventArgs, same as the ItemDataBound event). I still do not
seem to have access to any sort of collection or object representing the
column I want to hide.

Can you elaborate on how I can achieve this through this event?

Thanks again.

Jerad

Eliyahu Goldin said:
You can catch autocreated columns (an hide them) in ItemCreated event.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jerad Rose said:
I am working with a DataGrid (ASP.NET 2.0) that is set to
AutoGenerateColumns="true". It is being populated from a DataSet. There
is a column that I want to hide from the display, but I want to leave it
in the DataSet so that it can be referenced. However, all attempts that
I've tried are failing.

The main method I see being used is this:

myColumn.ColumnMapping = MappingType.Hidden

However, this isn't working -- the column remains visible.

If I do this:

myDataTable.Columns.Remove("MyColumn")

Then the column is hidden, but of course, this removes it from the
dataset.

Of course, I can't hide the column after the DataGrid is bound, because
the Columns collection isn't populated when using
AutoGenerateColumns="true" (the reason for this still baffles me, but
I've given up on figuring that one out).

I'm aware that I could hide each individual cell on ItemDataBound, but
this requires referencing the cell through a numeric index, which is a
poor practice, IMO (what if the order of the columns changes?).

Here is the full code block:

Dim myDataSet As DataSet = GetDataSet()

myDataSet.Tables("Table").Columns("MyColumn").ColumnMapping =
MappingType.Hidden

DataGrid1.DataSource = myDataTable
DataGrid1.DataBind()

Is there not a way to hide a column for a DataGrid that is using
AutoGenerateColumns="true", by referencing the column by name?

Thanks in advance for your help.

Jerad
 
J

Jerad Rose

Thanks again for your response, Eliyahu.

I mentioned this in my origninal post, but I prefer not to use this method,
as it requires that I use an ordinal reference and I feel this is a poor
practice, since it depends on the column order not changing. I would like
to find a solution that allows me to reference the column by name, and then
hide it.

Is this not possible with my situation?

Thanks.
Jerad

Eliyahu Goldin said:
In this event you should be able to locate the cell belonging to the
column
you want to hide. You can hide the column by hiding individual cells.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Jerad Rose said:
Thanks for the response.

Unfortunately, I don't see how I can use this event to hide a column. This
event is fired when creating row items, not columns (it passes
DataGridItemEventArgs, same as the ItemDataBound event). I still do not
seem to have access to any sort of collection or object representing the
column I want to hide.

Can you elaborate on how I can achieve this through this event?

Thanks again.

Jerad

Eliyahu Goldin said:
You can catch autocreated columns (an hide them) in ItemCreated event.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


I am working with a DataGrid (ASP.NET 2.0) that is set to
AutoGenerateColumns="true". It is being populated from a DataSet. There
is a column that I want to hide from the display, but I want to leave
it
in the DataSet so that it can be referenced. However, all attempts
that
I've tried are failing.

The main method I see being used is this:

myColumn.ColumnMapping = MappingType.Hidden

However, this isn't working -- the column remains visible.

If I do this:

myDataTable.Columns.Remove("MyColumn")

Then the column is hidden, but of course, this removes it from the
dataset.

Of course, I can't hide the column after the DataGrid is bound,
because
the Columns collection isn't populated when using
AutoGenerateColumns="true" (the reason for this still baffles me, but
I've given up on figuring that one out).

I'm aware that I could hide each individual cell on ItemDataBound, but
this requires referencing the cell through a numeric index, which is a
poor practice, IMO (what if the order of the columns changes?).

Here is the full code block:

Dim myDataSet As DataSet = GetDataSet()

myDataSet.Tables("Table").Columns("MyColumn").ColumnMapping =
MappingType.Hidden

DataGrid1.DataSource = myDataTable
DataGrid1.DataBind()

Is there not a way to hide a column for a DataGrid that is using
AutoGenerateColumns="true", by referencing the column by name?

Thanks in advance for your help.

Jerad
 
E

Eliyahu Goldin

There is an easily solution to this. In the same ItemCreated event detect
the Header item and locate in it's Cells collection the cell with the text
matching your column name. Remember the order of this cell and use it as an
index for data items.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jerad Rose said:
Thanks again for your response, Eliyahu.

I mentioned this in my origninal post, but I prefer not to use this
method, as it requires that I use an ordinal reference and I feel this is
a poor practice, since it depends on the column order not changing. I
would like to find a solution that allows me to reference the column by
name, and then hide it.

Is this not possible with my situation?

Thanks.
Jerad

Eliyahu Goldin said:
In this event you should be able to locate the cell belonging to the
column
you want to hide. You can hide the column by hiding individual cells.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Jerad Rose said:
Thanks for the response.

Unfortunately, I don't see how I can use this event to hide a column. This
event is fired when creating row items, not columns (it passes
DataGridItemEventArgs, same as the ItemDataBound event). I still do not
seem to have access to any sort of collection or object representing the
column I want to hide.

Can you elaborate on how I can achieve this through this event?

Thanks again.

Jerad

message You can catch autocreated columns (an hide them) in ItemCreated event.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


I am working with a DataGrid (ASP.NET 2.0) that is set to
AutoGenerateColumns="true". It is being populated from a DataSet. There
is a column that I want to hide from the display, but I want to leave
it
in the DataSet so that it can be referenced. However, all attempts
that
I've tried are failing.

The main method I see being used is this:

myColumn.ColumnMapping = MappingType.Hidden

However, this isn't working -- the column remains visible.

If I do this:

myDataTable.Columns.Remove("MyColumn")

Then the column is hidden, but of course, this removes it from the
dataset.

Of course, I can't hide the column after the DataGrid is bound,
because
the Columns collection isn't populated when using
AutoGenerateColumns="true" (the reason for this still baffles me, but
I've given up on figuring that one out).

I'm aware that I could hide each individual cell on ItemDataBound,
but
this requires referencing the cell through a numeric index, which is
a
poor practice, IMO (what if the order of the columns changes?).

Here is the full code block:

Dim myDataSet As DataSet = GetDataSet()

myDataSet.Tables("Table").Columns("MyColumn").ColumnMapping =
MappingType.Hidden

DataGrid1.DataSource = myDataTable
DataGrid1.DataBind()

Is there not a way to hide a column for a DataGrid that is using
AutoGenerateColumns="true", by referencing the column by name?

Thanks in advance for your help.

Jerad
 
E

Eliyahu Goldin

I don't know. There must be a reason for this. MS does care of the
developers and if a feature is not there there should be a reason.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jerad Rose said:
Thansk again, Eliyahu.

This sounds like it might work as a workaround, but it's definitely not a
very elegant approach. So it sounds like there is no built-in method that
.NET supports? Is there a reason why this ability isn't there? Is it an
oversight, or is it a bug?

For example, to me, it would make much more sense if either:

a) the DataGrid Columns collection *was* populated with autogenerated
columns, instead of being completely empty
b) setting the ColumnMapping to Hidden *did* work

Is there any logical explanation as to why neither of these is the case?

Thanks again.

Jerad

Eliyahu Goldin said:
There is an easily solution to this. In the same ItemCreated event detect
the Header item and locate in it's Cells collection the cell with the
text
matching your column name. Remember the order of this cell and use it as an
index for data items.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jerad Rose said:
Thanks again for your response, Eliyahu.

I mentioned this in my origninal post, but I prefer not to use this
method, as it requires that I use an ordinal reference and I feel this is
a poor practice, since it depends on the column order not changing. I
would like to find a solution that allows me to reference the column by
name, and then hide it.

Is this not possible with my situation?

Thanks.
Jerad

message In this event you should be able to locate the cell belonging to the
column
you want to hide. You can hide the column by hiding individual cells.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Thanks for the response.

Unfortunately, I don't see how I can use this event to hide a column.
This
event is fired when creating row items, not columns (it passes
DataGridItemEventArgs, same as the ItemDataBound event). I still do not
seem to have access to any sort of collection or object representing the
column I want to hide.

Can you elaborate on how I can achieve this through this event?

Thanks again.

Jerad

message You can catch autocreated columns (an hide them) in ItemCreated event.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


I am working with a DataGrid (ASP.NET 2.0) that is set to
AutoGenerateColumns="true". It is being populated from a DataSet.
There
is a column that I want to hide from the display, but I want to leave
it
in the DataSet so that it can be referenced. However, all attempts
that
I've tried are failing.

The main method I see being used is this:

myColumn.ColumnMapping = MappingType.Hidden

However, this isn't working -- the column remains visible.

If I do this:

myDataTable.Columns.Remove("MyColumn")

Then the column is hidden, but of course, this removes it from the
dataset.

Of course, I can't hide the column after the DataGrid is bound,
because
the Columns collection isn't populated when using
AutoGenerateColumns="true" (the reason for this still baffles me, but
I've given up on figuring that one out).

I'm aware that I could hide each individual cell on ItemDataBound,
but
this requires referencing the cell through a numeric index, which is
a
poor practice, IMO (what if the order of the columns changes?).

Here is the full code block:

Dim myDataSet As DataSet = GetDataSet()

myDataSet.Tables("Table").Columns("MyColumn").ColumnMapping =
MappingType.Hidden

DataGrid1.DataSource = myDataTable
DataGrid1.DataBind()

Is there not a way to hide a column for a DataGrid that is using
AutoGenerateColumns="true", by referencing the column by name?

Thanks in advance for your help.

Jerad
 
J

Jerad Rose

Thansk again, Eliyahu.

This sounds like it might work as a workaround, but it's definitely not a
very elegant approach. So it sounds like there is no built-in method that
..NET supports? Is there a reason why this ability isn't there? Is it an
oversight, or is it a bug?

For example, to me, it would make much more sense if either:

a) the DataGrid Columns collection *was* populated with autogenerated
columns, instead of being completely empty
b) setting the ColumnMapping to Hidden *did* work

Is there any logical explanation as to why neither of these is the case?

Thanks again.

Jerad

Eliyahu Goldin said:
There is an easily solution to this. In the same ItemCreated event detect
the Header item and locate in it's Cells collection the cell with the text
matching your column name. Remember the order of this cell and use it as an
index for data items.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


Jerad Rose said:
Thanks again for your response, Eliyahu.

I mentioned this in my origninal post, but I prefer not to use this
method, as it requires that I use an ordinal reference and I feel this is
a poor practice, since it depends on the column order not changing. I
would like to find a solution that allows me to reference the column by
name, and then hide it.

Is this not possible with my situation?

Thanks.
Jerad

Eliyahu Goldin said:
In this event you should be able to locate the cell belonging to the
column
you want to hide. You can hide the column by hiding individual cells.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Thanks for the response.

Unfortunately, I don't see how I can use this event to hide a column.
This
event is fired when creating row items, not columns (it passes
DataGridItemEventArgs, same as the ItemDataBound event). I still do not
seem to have access to any sort of collection or object representing the
column I want to hide.

Can you elaborate on how I can achieve this through this event?

Thanks again.

Jerad

message You can catch autocreated columns (an hide them) in ItemCreated event.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


I am working with a DataGrid (ASP.NET 2.0) that is set to
AutoGenerateColumns="true". It is being populated from a DataSet.
There
is a column that I want to hide from the display, but I want to leave
it
in the DataSet so that it can be referenced. However, all attempts
that
I've tried are failing.

The main method I see being used is this:

myColumn.ColumnMapping = MappingType.Hidden

However, this isn't working -- the column remains visible.

If I do this:

myDataTable.Columns.Remove("MyColumn")

Then the column is hidden, but of course, this removes it from the
dataset.

Of course, I can't hide the column after the DataGrid is bound,
because
the Columns collection isn't populated when using
AutoGenerateColumns="true" (the reason for this still baffles me, but
I've given up on figuring that one out).

I'm aware that I could hide each individual cell on ItemDataBound,
but
this requires referencing the cell through a numeric index, which is
a
poor practice, IMO (what if the order of the columns changes?).

Here is the full code block:

Dim myDataSet As DataSet = GetDataSet()

myDataSet.Tables("Table").Columns("MyColumn").ColumnMapping =
MappingType.Hidden

DataGrid1.DataSource = myDataTable
DataGrid1.DataBind()

Is there not a way to hide a column for a DataGrid that is using
AutoGenerateColumns="true", by referencing the column by name?

Thanks in advance for your help.

Jerad
 

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,015
Latest member
AmbrosePal

Latest Threads

Top