.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share

 Subscribe to Articles

How to get Hidden Column Value in GridView

Posted By :Nidhi Bhargava      Posted Date :07/05/2010   Points :25   Category: ASP.NET    URL: http://www.dotnetspark.com

In this article I am explaining different ways to get hidden column value in Gridview.
 


If Gridview has any BoundField column for which visible property is set to "false" then that columns won't be rendered at runtime and you can not get value of hidden column.

if you try to get value of any cell of hidden column by using GV.Rows[i].Cell[0].Text then it returns nothing.

But there are Four ways to get value of hidden column -

I am taking an example of gridview that display addresses and has first column for "AddressID" that is hidden column and I am defining different ways to get value of "AddressID".

1. First Way -

(a). In Gridview set DataKeyNames property to hidden column id.

Example:

In following case set DataKeyNams to "AddressID" and set visible false to first column that displays "AddressID".

<asp:GridView ID="GVAddress" runat="server" AutoGenerateColumns="False" DataKeyNames="AddressID"  >

      <Columns>

         <asp:BoundField DataField="AddressID" HeaderText="AddressID" Visible = "false" />

         <asp:BoundField DataField="AddressLine1" HeaderText="AddressLine1" SortExpression="AddressLine1" />

         <asp:BoundField DataField="AddressLine2" HeaderText="AddressLine2" SortExpression="AddressLine2" />

      </Columns>

</asp:GridView>

 

(b). Value of hidden Column i.e. AddressID can be get by using DayaKeys property of Gridview.

GVAddress.DataKeys[0].Value.ToString();

 

2. Second Way -

 

(a). Create TemplateField column for hidden column in GridView,Add Label inside <ItemTemplate> And set visible to false for <asp:TemplateField>.  So that TemplateField column won't be  displayed at runtime.

 

<asp:GridView ID=" GVAddress" runat="server" AutoGenerateColumns="False" >

  <Columns>

      <asp:TemplateField Visible=false>

         <ItemTemplate>

          <asp:Label id="lblAddressId" runat ="server" text='<%# Eval("AddressID")%>'></asp:Label>

         </ItemTemplate>

      </asp:TemplateField>

      <asp:BoundField DataField="AddressLine1" HeaderText="AddressLine1"       SortExpression="AddressLine1" />

      <asp:BoundField DataField="AddressLine2" HeaderText="AddressLine2" SortExpression="AddressLine2" />

  </Columns>

</asp:GridView>

 

(b). Use FindControl method of Row property of GridView to find Label control defined inside TemplateField column and assign it to label object and get value of label by using text property.

 

Label lblAddressId = (Label) GVAddress.Rows[index].FindControl("lblAddressId");

if (lblAddressId != null)

{

   string strAddressId = lblAddressId.Text;

}

 

3. Third Way -

(a). Create a stylesheet for hiding grid column.

 

<style type="text/css">

    .hideGridColumn

    {

        display:none;

    }

 </style>

 

(b). Set HeaderStyle-CssClass and ItemStyle-CssClass property of column to hideGridColumn, that is defined in css, to hide column "AddressID" column.

<asp:GridView ID="GVAddress" runat="server" AutoGenerateColumns="False" >

      <Columns>

         <asp:BoundField DataField="AddressID" HeaderText="AddressID"  HeaderStyle-CssClass=hideGridColumn ItemStyle-CssClass = hideGridColumn  />

         <asp:BoundField DataField="AddressLine1" HeaderText="AddressLine1" SortExpression="AddressLine1" />

         <asp:BoundField DataField="AddressLine2" HeaderText="AddressLine2" SortExpression="AddressLine2" />

      </Columns>

</asp:GridView>

 

(c). Get value of hidden column by using Cell property.

 

GVAddress.Rows[0].Cell[0].Text

 

4. Fourth Way -

Set visible to false of coulmn that is reuired to hide after bininging datasource to Gridview in Codebehind. In that case value of hidden column will be available by using GVAddress.Rows[0].Cell[0].Text.

GVAddress.DataSource = dtAddress;

GVAddress.DataBind();

GVAddress.Columns[0].Visible = false;

 



Featured Articles


Best Practices No 5: - Detecting .NET application memory leaks
Memory leaks in .NET application have always being programmer's nightmare. Memory leaks are biggest problems when it comes to production servers. Productions servers normally need to run with least down time. Memory leaks grow slowly and after sometime they bring down the server by consuming huge chunks of memory. Maximum time people reboot the system, make it work temporarily and send a sorry note to the customer for the downtime. ... Read More
.NET Best Practice No: 1:- Detecting High Memory consuming functions in .NET code
One of the important factors for performance degradation in .NET code is memory consumption. Many developers just concentrate on execution time to determine performance bottle necks in a .NET application. Only measuring execution time does not clearly give idea of where the performance issue resides. Ok, said and done one of the biggest task is to understand which function, assembly or class has consumed how much memory. In this tutorial we will see how we can find which functions consume how much memory. This article discusses the best practices involved using CLR profiler for studying memory allocation.... Read More
How to improve your LINQ query performance by 5 X times ?
LINQ has been criticized by many early adopters for its performance issues. Well if you are just going to drag and drop using DBML code generator I am sure you will land up in to mess. Try doing this make a simple LINQ to SQL project using DBML and see your SQL profiler, I am sure you will never like to touch DBML code generator again. ... Read More
Responses

No response found. Be the first to respond this post

Post Comment
You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend