Solved

RowUpdating Column Visible?

Posted on 2011-03-01
12
692 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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