Improve company productivity with a Business Account.Sign Up

x
?
Solved

RowUpdating Column Visible?

Posted on 2011-03-01
12
Medium Priority
?
700 Views
Last Modified: 2012-05-11
Q. Is it possible to make a column visible duing the OnRowUpdating event in code-behind?

1. I have a <asp:CommandField causing edit-mode to begin and would like make visible a hidden TextBox.
2. The TextBox text input during edit-mode is then used in SQL query.
0
Comment
Question by:pointeman
  • 6
  • 6
12 Comments
 
LVL 25

Expert Comment

by:Rouchie
ID: 35016585
Yes you can.

One approach would be to just put the TextBox inside an EditTemplate within a row.

Alternatively, in the RowUpdating event, first make the column visible and THEN rebind to your datasource.  ASP.NET won't render any column contents that are visible during the DataBind event, so make sure its visible before binding.
0
 

Author Comment

by:pointeman
ID: 35018861
Okay, I can HIDE the first column like so:

    protected void gridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
            gridView1.Columns[0].Visible = false;    
    }

However, I cannot SHOW the columns like so:

    protected void gridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
            gridView1.Columns[0].Visible = true;
    }
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 35018957
Try re-binding it to the database after setting visiblity.

    protected void gridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
            gridView1.Columns[0].Visible = true;
            gridView1.DataSource = whatever normally goes here...;
            gridView1.DataBind();
    }
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 25

Expert Comment

by:Rouchie
ID: 35018959
Sorry that should have said re-binding to the dataSOURCE
0
 

Author Comment

by:pointeman
ID: 35019424
Still not having any success.

I found these two links that use a stylesheet to hide then show a GridView column. It seems it's a matter of Rendering or not Rendering the column. Could be your code does the same thing but differently.  Have a look, it's interesting...

The first link states:

"If you do NOT want to RENDER the column at all, then you can set Visible property for that column to False."

"Now if you WANT to RENDER the column data but want to hide it from the user, then using Visible property will NOT work because setting that property will remove the column from the grid altogether."

http://www.netomatix.com/development/GridViewHideColumn.aspx

http://www.codeproject.com/Tips/80964/How-to-get-Hidden-Column-Value-in-GridView.aspx

0
 
LVL 25

Expert Comment

by:Rouchie
ID: 35019510
I would forget using stylesheets altogether because you are going to cause yourself all manner of problems.  Style sheets will only hide the rendered code, not the actual objects, so anyone can 'override' your rules and corrupt your page.

If you want to show a text box, put it in the appropriate ItemTemplate or EditItemTemplate area for that column.  Then it will get shown automatically when the GridView swtiches into view/edit mode.

I think what you are trying to do is hide a value that can then be used after the gridview row is edited (posted back) right?  If so, you are MUCH better off using the GridView.DataKeyNames functionality which can then be used with the selected row index to get the hidden data.
0
 

Author Comment

by:pointeman
ID: 35019780
Here's the GridView layout I should have stated to begin with.

[asp:BoundField]  [asp:TemplateField (TextBox/Label)]  [asp:CommandField]                                                            

If I could simply set the asp:TemplateField width to 0 THEN change the width to 100 after clicking the CommandField (edit-mode) Button.

This is a purely cosmetic purpose and some might find trivial, but I think it's a cool function if I can get it to work...
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 35019868
What exactly are you trying to show/hide?  Why is there a requirement to have it in the page even though its not visible?
0
 

Accepted Solution

by:
pointeman earned 0 total points
ID: 35032847
Okay, figured it out!

* Hide columns during normal activity
protected void gvLicPool_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    gridView1.Columns[8].HeaderStyle.Width = 1;
    gridView1.Columns[8].ItemStyle.Width = 1;
}

* Show columns during Edit-Mode
protected void gridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    gridView1.Columns[8].HeaderStyle.Width = 100;
    gridView1.Columns[8].ItemStyle.Width = 100;
}    
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 35034434
Width = 1 isn't actually hiding a column, its just setting its width to 1 pixel.  You never did explain what the end result is that you're aiming for...!
0
 

Author Comment

by:pointeman
ID: 35036354
>>Q. Is it possible to make a column visible duing the OnRowUpdating event in code-behind?

You may have taken the word visible by exact definition. I just don't want the column seen until RowUpdating occurs. Sorry if I confused anyone :)
0
 

Author Closing Comment

by:pointeman
ID: 35145545
This solution meets my needs by not displaying (hiding) the column and using Row events instead of reloading the page.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
There is a wide range of advantages associated with the use of ASP.NET. This is why this programming framework is used to create excellent enterprise-class websites, technologies, and web applications.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question