By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,823 Members | 756 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,823 IT Pros & Developers. It's quick & easy.

Hiding and making visible columns from a DataGrid

P: n/a
I have a datagrid that I want to add a new column to. This column will only
be visible under certain conditions. So I want to set the column
visible=false. Then when the right condition happens to change it to
visible=true.

You can't do that with a bound column (no ID), but you can create a
templatecolumn with a label.

To make these visible, I am going through each datagriditem and making them
visible after I have bound the data to to the datagrid.

Is there a way to do this before I bind the data or to say set all the rows
to visible with one statement?

Thanks,

Tom
Nov 19 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
tshad wrote:
I have a datagrid that I want to add a new column to. This column will only
be visible under certain conditions. So I want to set the column
visible=false. Then when the right condition happens to change it to
visible=true.

You can't do that with a bound column (no ID), but you can create a
templatecolumn with a label.

To make these visible, I am going through each datagriditem and making them
visible after I have bound the data to to the datagrid.

Is there a way to do this before I bind the data or to say set all the rows
to visible with one statement?

Thanks,

Tom


Hmmm, are you basing this on the column wizard in VS.NET? You can
control visiblity programmatically using the .Visible property of hte
DataGridColumn. A couple examples:

http://msdn.microsoft.com/library/de...sibletopic.asp

http://www.c-sharpcorner.com/Code/20...ridColDyna.asp

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Nov 19 '05 #2

P: n/a
"Craig Deelsnyder" <cdeelsny@NO_SPAM_4_MEyahoo.com> wrote in message
news:ul**************@TK2MSFTNGP10.phx.gbl...
tshad wrote:
I have a datagrid that I want to add a new column to. This column will
only be visible under certain conditions. So I want to set the column
visible=false. Then when the right condition happens to change it to
visible=true.

You can't do that with a bound column (no ID), but you can create a
templatecolumn with a label.

To make these visible, I am going through each datagriditem and making
them visible after I have bound the data to to the datagrid.

Is there a way to do this before I bind the data or to say set all the
rows to visible with one statement?

Thanks,

Tom
Hmmm, are you basing this on the column wizard in VS.NET? You can control
visiblity programmatically using the .Visible property of hte
DataGridColumn. A couple examples:

http://msdn.microsoft.com/library/de...sibletopic.asp

http://www.c-sharpcorner.com/Code/20...ridColDyna.asp


They weren't exactly what I was looking for, but close. They did, however,
point me in the direction I needed.

What I found was that I could look at the column class of the datagrid, find
the headertext that equaled the column I was interested in and then do
visible=false (or true) on that column (before or after the bind).

Works great.

Here is the code I used:

for each column as datagridcolumn in oGrid.Columns
if column.headertext = "Rank" then
column.visible=False
exit for
end if
next

Simple.

Thanks,

Tom
--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET

Nov 19 '05 #3

P: n/a
Any ideas how to Check or uncheck a checkbox to automativally hide or show
columns in a datagrid?
"tshad" wrote:
"Craig Deelsnyder" <cdeelsny@NO_SPAM_4_MEyahoo.com> wrote in message
news:ul**************@TK2MSFTNGP10.phx.gbl...
tshad wrote:
I have a datagrid that I want to add a new column to. This column will
only be visible under certain conditions. So I want to set the column
visible=false. Then when the right condition happens to change it to
visible=true.

You can't do that with a bound column (no ID), but you can create a
templatecolumn with a label.

To make these visible, I am going through each datagriditem and making
them visible after I have bound the data to to the datagrid.

Is there a way to do this before I bind the data or to say set all the
rows to visible with one statement?

Thanks,

Tom


Hmmm, are you basing this on the column wizard in VS.NET? You can control
visiblity programmatically using the .Visible property of hte
DataGridColumn. A couple examples:

http://msdn.microsoft.com/library/de...sibletopic.asp

http://www.c-sharpcorner.com/Code/20...ridColDyna.asp


They weren't exactly what I was looking for, but close. They did, however,
point me in the direction I needed.

What I found was that I could look at the column class of the datagrid, find
the headertext that equaled the column I was interested in and then do
visible=false (or true) on that column (before or after the bind).

Works great.

Here is the code I used:

for each column as datagridcolumn in oGrid.Columns
if column.headertext = "Rank" then
column.visible=False
exit for
end if
next

Simple.

Thanks,

Tom

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET


Nov 19 '05 #4

P: n/a
I can Show or hide the first column depending on the value of the check
box by doing below.

But is it possible to hide or show a particular ROW?

if (ShowCheckBox.Checked)
{
ItemsGrid.Columns[0].Visible = true;

}
else
{
ItemsGrid.Columns[0].Visible = false;
}

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #5

P: n/a
> I have a datagrid that I want to add a new column to. This column will
only
be visible under certain conditions. So I want to set the column
visible=false. Then when the right condition happens to change it to
visible=true. On the client or on the server?
You can't do that with a bound column (no ID), Why? I am doing it all the time. What do you mean by no ID?
but you can create a
templatecolumn with a label.

To make these visible, I am going through each datagriditem and making them visible after I have bound the data to to the datagrid.

Is there a way to do this before I bind the data or to say set all the rows to visible with one statement? You can operate with CssClass property. css rule display:none is
recommended.

Thanks,

Tom

Nov 19 '05 #6

P: n/a
"Patrick.O.Ige" <Pa*********@discussions.microsoft.com> wrote in message
news:82**********************************@microsof t.com...
Any ideas how to Check or uncheck a checkbox to automativally hide or show
columns in a datagrid?
Here is what I do to check for a Row in a Datagrid that I have changed to
Green, the Label is set to yellow and the RadioButton is checked. I then
change the Row to empty (the original background will now be showing),
change the Label from yellow to black and uncheck the RadioButton.

DataGrid1 is the ID of the DataGrid. The for/each loop is going through all
the DataGrid items (rows). The FindControl method will look on that row to
find, in our case, the Label called "Answer". You need to set a Label
object pointer to point at the Label object and you need a RadioButton
object to point at the Radio Button.

Dim Answer as Label

for each oDGI as DataGridItem in DataGrid1.Items
if oDGI.BackColor.Equals(Color.Green) then
oDGI.BackColor = System.Drawing.Color.Empty
Answer = CType(oDGI.FindControl("lblAnswer"),Label) '
lblAnswer is the ID of the Label
Answer.ForeColor = System.Drawing.Color.Black
Dim rowSelected as RadioButton =
CType(oDGI.FindControl("rowSelected"),RadioButton) 'rowSelected is ID
rowSelected.checked = false
end if
next

You do exactly the same thing for a Checkbox. It would look something like:

Dim theCheckBox as CheckBox =
CType(oDGI.FindControl("rowSelected"),CheckBox) 'rowSelected is ID
theCheckBox.checked = false

This BTW works exactly the same for DataLists where you change DataGridItem
to DataListItem in the for/each loop.

As you can see from post (at the bottom), I do the same thing for columns,
except I use DataGridColumn in place of DataGridItem.

Tom

"tshad" wrote:
"Craig Deelsnyder" <cdeelsny@NO_SPAM_4_MEyahoo.com> wrote in message
news:ul**************@TK2MSFTNGP10.phx.gbl...
> tshad wrote:
>> I have a datagrid that I want to add a new column to. This column
>> will
>> only be visible under certain conditions. So I want to set the column
>> visible=false. Then when the right condition happens to change it to
>> visible=true.
>>
>> You can't do that with a bound column (no ID), but you can create a
>> templatecolumn with a label.
>>
>> To make these visible, I am going through each datagriditem and making
>> them visible after I have bound the data to to the datagrid.
>>
>> Is there a way to do this before I bind the data or to say set all the
>> rows to visible with one statement?
>>
>> Thanks,
>>
>> Tom
>
> Hmmm, are you basing this on the column wizard in VS.NET? You can
> control
> visiblity programmatically using the .Visible property of hte
> DataGridColumn. A couple examples:
>
> http://msdn.microsoft.com/library/de...sibletopic.asp
>
> http://www.c-sharpcorner.com/Code/20...ridColDyna.asp


They weren't exactly what I was looking for, but close. They did,
however,
point me in the direction I needed.

What I found was that I could look at the column class of the datagrid,
find
the headertext that equaled the column I was interested in and then do
visible=false (or true) on that column (before or after the bind).

Works great.

Here is the code I used:

for each column as datagridcolumn in oGrid.Columns
if column.headertext = "Rank" then
column.visible=False
exit for
end if
next

Simple.

Thanks,

Tom
>
> --
> Craig Deelsnyder
> Microsoft MVP - ASP/ASP.NET


Nov 19 '05 #7

P: n/a
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:OP**************@TK2MSFTNGP14.phx.gbl...
I have a datagrid that I want to add a new column to. This column will only
be visible under certain conditions. So I want to set the column
visible=false. Then when the right condition happens to change it to
visible=true.

On the client or on the server?


Server
You can't do that with a bound column (no ID), Why? I am doing it all the time. What do you mean by no ID?


No. You can't set an ID on a Boundcolumn. You need to set a templatecolumn
and then a label or textbox to set an ID (at least that is what MS Docs
say). And when I try, I get the following error:

Type 'System.Web.UI.WebControls.BoundColumn' does not have a property named
'id'
but you can create a
templatecolumn with a label.

To make these visible, I am going through each datagriditem and making them
visible after I have bound the data to to the datagrid.

Is there a way to do this before I bind the data or to say set all the

rows
to visible with one statement?

You can operate with CssClass property. css rule display:none is
recommended.


Why?????

Visible is a valid Property of the asp.net objects.

Tom

Thanks,

Tom


Nov 19 '05 #8

P: n/a
> >> You can't do that with a bound column (no ID),
Why? I am doing it all the time. What do you mean by no ID?
No. You can't set an ID on a Boundcolumn. You need to set a

templatecolumn and then a label or textbox to set an ID (at least that is what MS Docs
say). And when I try, I get the following error:

Type 'System.Web.UI.WebControls.BoundColumn' does not have a property named 'id'

Right, but what do you need it for? BoundColumn does have Visible property
and you don't need any id to set it to false.
You can operate with CssClass property. css rule display:none is
recommended.


Why?????

Visible is a valid Property of the asp.net objects.

Right. Just note that columns with Visible=false don't get rendered to the
client and come back empty. That is important if you need to update records.

Eliyahu
Nov 19 '05 #9

P: n/a
"Eliyahu Goldin" <re*************@monarchmed.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
>> You can't do that with a bound column (no ID),
> Why? I am doing it all the time. What do you mean by no ID?
No. You can't set an ID on a Boundcolumn. You need to set a

templatecolumn
and then a label or textbox to set an ID (at least that is what MS Docs
say). And when I try, I get the following error:

Type 'System.Web.UI.WebControls.BoundColumn' does not have a property

named
'id'

Right, but what do you need it for? BoundColumn does have Visible property
and you don't need any id to set it to false.
> You can operate with CssClass property. css rule display:none is
> recommended.


Why?????

Visible is a valid Property of the asp.net objects.

Right. Just note that columns with Visible=false don't get rendered to the
client and come back empty. That is important if you need to update
records.


Agreed.

But even when not rendered, fields such as Labels and Textboxes, are still
there and can be manipulated (on the Server side).

Tom
Eliyahu

Nov 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.