Hiding a column

D

dev648237923

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!
 
S

Steven Cheng[MSFT]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top