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

Posted by in ASP.NET category on for Intermediate level | Views : 10443 red flag

when a user selects a row in a Grid that selected Row Hiding column value have to retrive.

     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" >

<asp:CommandField ShowSelectButton="True" />

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

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

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

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

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Syed Shakeer Hussiain P
Member Level:
Member Status: Member
Member Since: 2/5/2009 3:12:18 AM
Country: India
Syed Shakeer Hussain
Shakeer Hussain has completed his Master of Computer Applications degree from Deccan College of engg and technology of Osmania University.He is a MVM of has good experience in the areas of ASP.NET, C#.NET, VB.NET, SQL SERVER 2000/2005 and Windows Mobile. He has worked in Windows Mobile,Web Applicatin and ERP projects.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)