Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / DataGrid / November 2006

Tip: Looking for answers? Try searching our database.

Hiding a column

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dev648237923 - 17 Nov 2006 17:06 GMT
I do my updating in the code-behind (long story). I need an ID field in my
DtatGrid to pass to my Stored Proceedure but I do not want that column to
show up on the screen. If I set the GridView column to Visible="false" then
in the code behind when I need to retrieve the ID the below gives an error:
int MyID = int.Parse(GridView1.Rows[e.RowIndex].Cells[1].Text);

So I came up with what I think works well for hiding the column but still
leaving it visible to the code behind:
In the asp:DataBound tag I do: HeaderStyle-CssClass="hidetd"
ItemStyle-CssClass="hidetd"

Then in my stylesheet I have:
.hidetd { display: none; }

This causes the column not to show on the screen but to still be there so I
can get items from it with GridView1.Rows[e.RowIndex].Cells[1].Text.

I would appretiate if there is a better way or if this way has any unforseen
flaws to hear your comments.

Thank you!
Eliyahu Goldin - 19 Nov 2006 08:32 GMT
This is the correct way.

Signature

Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]

> I do my updating in the code-behind (long story). I need an ID field in my
> DtatGrid to pass to my Stored Proceedure but I do not want that column to
[quoted text clipped - 18 lines]
>
> Thank you!
Steven Cheng[MSFT] - 20 Nov 2006 06:02 GMT
Hi dev648237923,

As you said that you're currently manually performing the update in code
behind, then how do you retrieve the data from GridView Row(being edit),
manually get the Cell's InnerText or Find the inner control and read value?

If you're already used the "Findcontrol and read value from inner control"
approah, you can also consider using this way to store and retrieve the ID
field in GridView. To this, you can follow the below steps:

1. Add an html input hidden control(mark as runat="server") into the
tempatefield with other control  and bind the ID data to this html hidden
field's "value" property. e.g.

=====================
<asp:TemplateField HeaderText="Description" SortExpression="Description">
                   <EditItemTemplate>
                       <asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("Description") %>'></asp:TextBox>
                        <input type="hidden" runat="server" id="hdID"
value='<%# Bind("CategoryID") %>' />
                   </EditItemTemplate>
                   <ItemTemplate>
                       <asp:Label ID="Label1" runat="server" Text='<%#
Bind("Description") %>'></asp:Label>
                       <input type="hidden" runat="server" id="hdID"
value='<%# Bind("CategoryID") %>' />
                   </ItemTemplate>
               </asp:TemplateField>
========================

2.  In code behind event, you can Find this hidden field and retrieve ID
value from it for updating.

===============
protected void GridView1_RowUpdating(object sender,
GridViewUpdateEventArgs e)
   {
       GridViewRow row = GridView1.Rows[e.RowIndex];

       HtmlInputHidden hidden = row.FindControl("hdID") as HtmlInputHidden;

       Response.Write("<br/>ID: " + hidden.Value);
     
   }
================

Also, there are other means to do so. for example, you can also add the ID
value into an existing control's Attributes collection in GridView's
"RowDataBound" event and retrieve it later. e.g.

=====================
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           e.Row.Attributes["ID"] = xxxx;
       }
   }
==========================

Hope this helps. If you have anything unclear on this, please feel free to
post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.