Solved

RowUpdating Column Visible?

Posted on 2011-03-01
12
689 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now