Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

RowUpdating Column Visible?

Posted on 2011-03-01
12
693 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

839 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