Go to DotNetFunda.com
 Online : 742 |  Welcome, Guest!   Login
 
Home > Articles > ASP.NET > How to get the value of a Hidding column in a Gridview using C#

  • Nominate yourself for "Agile Software Development using Scrum" online session for FREE, click here.

  • Download OOPS and ASP.NET Online training session video and PPT from here.

Submit Article | Articles Home | Search Articles |

How to get the value of a Hidding column in a Gridview using C#

 Posted on: 9/8/2009 6:05:36 PM by Syedshakeer | Views: 955 | Category: ASP.NET | Level: Intermediate | Print Article
when a user selects a row in a Grid that selected Row Hiding column value have to retrive.

Buy online comprehensive training video pack just for $35.00 only, see what's inside it.

     How to Hide a column in a Gridview  :-

    To Hide a column we have to use a hidden filed as follows:

eg: <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("ProductId") %>' />

   Above i am using a Value attribute to retrive a data from a 'ProductId column.

   Here i am displaying 4 columns values in a Gridview.
   In a Gridview i am using 'select 'link,when a user selects a row in Grid that selected Row id value have to retrive.

   How to create a' select 'link column in Gridview :-

 'Select' Link  can be build by using 'CommandField' as follows:

<asp:CommandField ShowSelectButton="True" />
   In .aspx page


<asp:GridView ID="Gridivew1" runat ="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AllowPaging="True" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" OnRowDataBound="Gridivew1_RowDataBound" OnSelectedIndexChanged="Gridivew1_SelectedIndexChanged" >
<Columns>

<asp:CommandField ShowSelectButton="True" />

<asp:TemplateField HeaderText ="ProductName">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("ProductId") %>' />

<asp:Label ID ="lblProductName1" runat ="server" Text ='<%#Eval("ProductName")%>'>
</asp:Label>

</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="ProductName">
<ItemTemplate>
<asp:Label ID ="lblQuantityPerUnit" runat ="server" Text ='<%#Eval("QuantityPerUnit")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="UnitPrice">
<ItemTemplate>
<asp:Label ID ="lblUnitPrice" runat ="server" Text ='<%#Eval("UnitPrice")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>

</Columns>
<RowStyle BackColor="White" ForeColor="#330099" />
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />

</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Products]"></asp:SqlDataSource>

  Next Select your Gridview Events.Double click on SelectedIndexChanged Event and write the Below code  in .aspx.cs page:

protected void Gridivew1_SelectedIndexChanged(object sender, EventArgs e)
{
string strValue = ((HiddenField)Gridivew1.SelectedRow.Cells[1].FindControl("HiddenField1")).Value;
string strValue2 = ((Label)Gridivew1.SelectedRow.Cells[1].FindControl("lblProductName1")).Text ;


Response.Write("Product Id=" + strValue + "product=" + strValue2);



}

   Thanks for reading my article!

    Syed Shakeer Hussain


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:1 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, February 05, 2009
Level:Bronze
Status: [Member]
Biography:Hi to All ..
I am Working as Software Developer on .NET and MVM of www.dotnetspider.com
 Latest post(s) from Syedshakeer

   ◘ How to get the value of a Hidding column in a Gridview using C# posted on 9/8/2009 6:05:36 PM
   ◘ How to calculate total at the BackEnd using Trigger? posted on 8/25/2009 6:14:14 PM
   ◘ How to display records as First-Next-Previous-Last in a Textboxes using Windows Application? posted on 8/25/2009 6:01:05 PM
   ◘ How to Start Mobile Application on Windows? posted on 8/18/2009 1:00:01 AM
   ◘ Paging for First, Next, Previous and Last in gridview posted on 8/16/2009 12:47:43 PM


Submit Article

About Us | The Team | Advertise | Contact Us | Feedback | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found copied contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)