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


Top 5 Contributors of the Month
david stephan

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: May 07, 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"  >

     

        

        

        

     

 

(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.

 

 

     

        

         

        

     

      

     

 

 

(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.

 

 

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

     

           HeaderStyle-CssClass=hideGridColumn ItemStyle-CssClass = hideGridColumn  />

        

        

     

 

(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;

 


 Subscribe to Articles

     

Further Readings:

Responses
Author: ram         Company URL: http://www.dotnetspark.com
Posted Date: November 17, 2010

Awesome article.. you saved my life

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