Hiding and making visible columns from a DataGrid

T

tshad

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
 
C

Craig Deelsnyder

tshad said:
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/d...ebcontrolsdatagridcolumnclassvisibletopic.asp

http://www.c-sharpcorner.com/Code/2003/June/HideDataGridColDyna.asp
 
T

tshad

Craig Deelsnyder said:
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/d...ebcontrolsdatagridcolumnclassvisibletopic.asp

http://www.c-sharpcorner.com/Code/2003/June/HideDataGridColDyna.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
 
G

Guest

Any ideas how to Check or uncheck a checkbox to automativally hide or show
columns in a datagrid?


tshad said:
Craig Deelsnyder said:
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/d...ebcontrolsdatagridcolumnclassvisibletopic.asp

http://www.c-sharpcorner.com/Code/2003/June/HideDataGridColDyna.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
 
P

Patrick Olurotimi Ige

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;
}
 
E

Eliyahu Goldin

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.
 
T

tshad

Patrick.O.Ige said:
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
 
T

tshad

Eliyahu Goldin said:
On the client or on the server?
Server

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'
You can operate with CssClass property. css rule display:none is
recommended.

Why?????

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

Tom
 
E

Eliyahu Goldin

You can't do that with a bound column (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.
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
 
T

tshad

Eliyahu Goldin said:
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.

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
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top