473,395 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Hiding columns in GridView

Hi all,

Hopefully this one is rather simple. I just haven't been able to figure it out.

On my GridView I have some hidden key columns which I've hidden by using tha attribute Visible="false" in the TemplateField declaration. This column is later used as an UpdateParameter field.

Now if I leave the field as Visible="true", everything works fine and down in my C# code, I can easily access the UpdateParameter field with e.Command.Parameters[0].Value. However, when I set Visible to false, the call to e.Command.Parameters[0].Value always returns null.

So how can I hide this column from the users, but still use it as a parameter field?

Thanks for any assistance.

Robert
Apr 22 '08 #1
9 2877
Frinavale
9,735 Expert Mod 8TB
Hi all,

Hopefully this one is rather simple. I just haven't been able to figure it out.

On my GridView I have some hidden key columns which I've hidden by using tha attribute Visible="false" in the TemplateField declaration. This column is later used as an UpdateParameter field.

Now if I leave the field as Visible="true", everything works fine and down in my C# code, I can easily access the UpdateParameter field with e.Command.Parameters[0].Value. However, when I set Visible to false, the call to e.Command.Parameters[0].Value always returns null.

So how can I hide this column from the users, but still use it as a parameter field?

Thanks for any assistance.

Robert
I ran into the same problem once.
It's because when you user the .NET Visible=False the field is never rendered in browser...therefore when it comes back to the server and you try to access it there isn't anything there.

What I've done is used CSS to hide the column.
I set the CssClass of the item to be the Css class "noShow"
eg:
Expand|Select|Wrap|Line Numbers
  1. <asp:BoundField HeaderText="theIndex" DataField="theIndex">
  2.      <ItemStyle CssClass="noShow" Width="0px" />
  3.      <HeaderStyle CssClass="noShow" Width="0px" />
  4.   </asp:BoundField>
  5.  
Where my 'noShow" Css class was
Expand|Select|Wrap|Line Numbers
  1. .noShow
  2. {
  3.      display:none;
  4. }
  5.  
-Frinny
Apr 22 '08 #2
Works beautifully. Thanks!

Robert
Apr 22 '08 #3
One anomaly I noticed with this idea… it doesn’t seem to hide the FooterTemplate columns. I’ll continue to try to figure it out, but if you have any clues, please let me know.

Robert
Apr 22 '08 #4
DOH! I feel like a doofus.

<FooterStyle CssClass="noShow" Width="0px" />

Solved my problem.

Robert
Apr 22 '08 #5
Frinavale
9,735 Expert Mod 8TB
DOH! I feel like a doofus.

<FooterStyle CssClass="noShow" Width="0px" />

Solved my problem.

Robert
Don't feel too bad, it took me a while to even think about using CSS to format the look of my GridView.

-Frinny
Apr 22 '08 #6
Okay, here's one that's similar, but I haven't been able to solve.

I want to dynamically hide the columns. In other words, during the page_load I get information about the current user, so later (on Page_Load, OnPageRender, OnRowDataBound-- I'm not sure) I want to selectively hide some columns from the user based upon information I'd earlier gathered during the page_load. I can't do this directly in the ASP code, so I'm assuming I'll do it down in the C# code by setting the Itemstyle, Headerstyle and Footerstyle of the TemplateField.

How does one do this? I've been trying for the last two hours to figure it out, but I'm at a loss.

Robert
Apr 24 '08 #7
Plater
7,872 Expert 4TB
You can either do a post process after the data has been loaded into the columns or you can do something in the RowDataBound event.

As for the original problem, seems like you can access the row directly from like mygridview.Rows[<whatever>] and get the value of the hidden column. In the click events (and others) you are usualy given the row index value so you can find the correct row and get the value from your hidden column through that method.
Apr 24 '08 #8
Frinavale
9,735 Expert Mod 8TB
Okay, here's one that's similar, but I haven't been able to solve.

I want to dynamically hide the columns. In other words, during the page_load I get information about the current user, so later (on Page_Load, OnPageRender, OnRowDataBound-- I'm not sure) I want to selectively hide some columns from the user based upon information I'd earlier gathered during the page_load. I can't do this directly in the ASP code, so I'm assuming I'll do it down in the C# code by setting the Itemstyle, Headerstyle and Footerstyle of the TemplateField.

How does one do this? I've been trying for the last two hours to figure it out, but I'm at a loss.

Robert
It's probably easiest in your RowDataBound event.

Expand|Select|Wrap|Line Numbers
  1. Protected Sub MyGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GV_MyGridView.RowDataBound
  2.  
  3.      If myUser.IsAllowedToViewMyFirstColumn() = False Then
  4.          e.Row.Cells(0).CssClass = "noShow"
  5.      End If
  6. End Sub
  7.  
Expand|Select|Wrap|Line Numbers
  1. Protected Void MyGridView_RowDataBound(Object sender,System.Web.UI.WebControls.GridViewRowEventArgs e) 
  2. {
  3.      If (myUser.IsAllowedToViewMyFirstColumn() == False)
  4.      {     e.Row.Cells[0].CssClass = "noShow";}
  5. }
  6.  
-Frinny
Apr 24 '08 #9
Okay, you learn something new every day. I had assumed-- falsely apparently-- that the OnRowBound event would only affect the specific row on which it was called. I needed to hide the header and footer as well and since these are not necessarily bound (my assumption-- maybe they are), it would not fire the OnRowBound event. But it works as you describe.

Many thanks for the responses.

Robert
Apr 24 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: srinivas | last post by:
Hi, I have binded a dataset to a gridview control at run time. I'm fetching 5 columns from the database. But while displaying it on the screen i want to show only 3 columns. I tried with...
2
by: =?Utf-8?B?Sm9zaCBTY2htaWR0?= | last post by:
I have a gridview that is being used for managing inventory. The default view shows the stock currently available. When editing I don't want the stock to be directly edited, rather the user will...
0
by: Frank | last post by:
Hi, I have read the other post about hiding the updatepanel using the ajax lifecycle as such at the end of the aspx page: <script type="text/javascript" language =javascript> var c = new...
1
by: Dica | last post by:
i need to allow the user to select a row from my dataGrid for editing. as such, i include the record ID in the first column and then extract like so when retrieving the record details: protected...
2
by: =?Utf-8?B?SmF5IFBvbmR5?= | last post by:
Based on wether a row is selected in a GridView I need to HIDE the last two columns of a gridview. I do NOT need to make the cells invisible I want to hide the entire column. When I set the...
0
by: hedgracer | last post by:
I have a gridview that has six columns. I need to hide the last column (i.e. not download it) in the download from gridview to excel. My current code (which downloads the entire gridview) is as...
1
by: taa | last post by:
protected void LoadExercises(object sender, EventArgs e) { // Navn, (Beskrivelse), Type, Exid, Target var exercises = from ex in context.Exercises select new {exerciseId = ex.ExerciseId,...
2
by: Ralf | last post by:
I have a custome request to print a gridview with lots of columns, 31 to be exact. I know how to print out a gridview, done it already for a GV with 6 columns. But, this GV is very wide and will...
9
by: ally | last post by:
Hi, I am programmatically binding gridview to a datatable. I want to hide 2 columns. I can be done by adding code in RowCreated event of gridview like e.Row.Cells.Visible = false; ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.