Hide a column in asp:GridView

G

ghostwolf

Hi, I want to hide a column in the asp:GridView, say one of the column of
asp:BoundField. But it is not allowed to put <div> inside for setting it
display:none. What can I do? Thanks in millions.
 
M

Mark Rae

Hi, I want to hide a column in the asp:GridView, say one of the column of
asp:BoundField. But it is not allowed to put <div> inside for setting it
display:none. What can I do? Thanks in millions.

MyGridView.Columns[0].Visible = false;

HOWEVER, bear in mind that this isn't actually *hiding* the column per se -
it's physically *removing* the column from the HTML being sent down to the
client, which may not be what you really want...
 
G

ghostwolf

thanks, actually I don't really want to invisible it. It is because I want
to put a value for each row but make it hide away. Just like what we did in
windows programming for adding a value for each row. If I make the column in
asp:GridView invisible, I cannot get the value from that column.

Mark Rae said:
Hi, I want to hide a column in the asp:GridView, say one of the column of
asp:BoundField. But it is not allowed to put <div> inside for setting it
display:none. What can I do? Thanks in millions.

MyGridView.Columns[0].Visible = false;

HOWEVER, bear in mind that this isn't actually *hiding* the column per
se - it's physically *removing* the column from the HTML being sent down
to the client, which may not be what you really want...
 
M

Mark Rae

thanks, actually I don't really want to invisible it. It is because I want
to put a value for each row but make it hide away. Just like what we did
in windows programming for adding a value for each row. If I make the
column in asp:GridView invisible, I cannot get the value from that column.

Ah... You've discovered the additional security feature that a GridView has
but a DataGrid didn't.

Basically, Microsoft took the view that using hidden columns to store data
was a potential security risk, so they removed hidden colums from ViewState
at databind time. However, it's easy enough to work round, but be aware of
the potential security risk involved in doing this...

MyGridView.DataSource = <datasource>;
MyGridView.Columns[0].Visible = true;
MyGridView.DataBind();
MyGridView.Columns[0].Visible = false;
 
W

Winista

Thank you for pointing out the issue with the approach. It has been fixed on
the site as well as attached demo project.
 
Joined
Jun 8, 2009
Messages
1
Reaction score
0
columns unavailable after databind

I tried GridView2.Columns(2).Visible = false and got an error message. I used the immediate window for: ?GridView2.Columns.Count and the result was 0. I stepped past that operation and the grid displays 3 columns. What's going on?
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top