Access GridView Invisible Columns
By AzamSharp
Views: 34326

Introduction:

I have been getting a lot of emails in which I was being asked to demonstrate that how one can retrieve the value of an invisible column of the GridView control. In this article I will show you two ways that you can use to retrieve the value from an invisible column.

Why Invisible Column? 

One might ask that why should I use an invisible column in the first place. There are many reasons of making the column invisible. You might want to use a column as the primary key which retrieves the value from the database and display it using the GridView control. Since, primary key is a confidential data you want might to hide it from the users. Another reason of making the column invisible is that you might want to have some additional information to save an extra trip to the database. Please note that the second scenario should not be used to display many columns as this will increase the View State and thus the size of the page large. 

Using the DataKeys Property:

The simplest way to access the primary key is by using DataKeys property of the GridView control. DataKeys property represents the column which is to be used as the primary key. In this article I will use my custom database "Tasks" and display the columns "Title", "Description", "DateCreated" in the GridView control. Apart from the columns from the database the GridView also contains a CheckBox Template Column which is used to check the tasks which are completed. Take a look at the HTML below to have a clear idea.

<asp:GridView ID="gvInComplete" runat="server" AutoGenerateColumns="False" CellPadding="4"

ForeColor="#333333" GridLines="None" DataKeyNames="TaskID">

<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />

<Columns>

<asp:BoundField DataField="Title" HeaderText="Title" />

<asp:BoundField DataField="Description" HeaderText="Description" />

<asp:BoundField DataField="DateCreated" HeaderText = "Date Created" />

<asp:TemplateField HeaderText="Select">

<ItemTemplate>

<asp:CheckBox ID="chkSelect" runat="server" />

</ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

 

As, you can see in the code above the DataKeyNames property is set to "TaskID" which, is the primary key in the table. Now, let's see how we can access all the TaskID of the rows which are checked using the CheckBoxes.

DataKey key;

foreach (GridViewRow row in gvInComplete.Rows)

{

bool result = ((CheckBox)row.FindControl("chkSelect")).Checked;

if (result)

{

key = gvInComplete.DataKeys[row.RowIndex];

Response.Write((int)key.Value);

}

}

In the code above I am iterating through all the rows in the GridView control. If I find a row that is checked then I gets the primary key of the row using the GridView DataKeys collection. The Row class property RowIndex will contain the index of the current row.

This is pretty simple right! But what if I want to access another column which is not a primary key. This can be done by using a Template Column inside the GridView control.

Accessing Invisible Column Using Template Field:

To access the invisible column using Template Field is very straight forward. All you need to do is to make the Template Field invisible and use the control inside the Template Field to access the values. Check out the following HTML code:

<asp:TemplateField Visible="False">

<ItemTemplate>

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

</ItemTemplate>

</asp:TemplateField>

In the above HTML code I have simply defined a Label control inside the ItemTemplate property of the Template Field. The Template Field is made invisible so, the user will not see it on when it is bound to the GridView control. You can access the TaskID using the following code:

int taskID = 0;

Task task = new Task();

foreach (GridViewRow row in gvInComplete.Rows)

{

bool result = ((CheckBox) row.FindControl("chkSelect")).Checked;

if (result)

{

taskID = Convert.ToInt32(((Label)row.FindControl("lblTaskID")).Text);

task.UpdateTask(taskID);

}

}

In the above code I am simply iterating through the GridView rows and when I find that the checkbox is checked then I get the value from the Label control which in this case is TaskID. After, I get the TaskID I can perform any function on it in my case UpdateTask.

I hope you liked the article, happy coding!


By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: .
Name: Isaac
Date: 1/27/2007 4:04:31 AM
Comment:
Thanks for article, was very helpful :-)
Subject: Thank You
Name: Tussan
Date: 1/31/2007 1:57:59 AM
Comment:
Just what I needed!!!
Subject: Thank you
Name: Valentyna
Date: 2/1/2007 12:49:53 PM
Comment:
Thank you very much. Your article helped me a lot.
Subject: Asp.net : gridview
Name: Krunal Joshi
Date: 2/2/2007 4:45:17 PM
Comment:
it was too easy to find out how hidden columns can be accessed .
That article was too good
Subject: Comments
Name: Balamurugan
Date: 2/9/2007 5:39:55 AM
Comment:
Thanks
your article is very nice
Subject: awesome
Name: zeffy
Date: 2/18/2007 5:46:33 PM
Comment:
Awesome! The article made a lot of sense... and helped me to work out an issue i had with gridviews!
Subject: Article is very helpful
Name: Swarup Ballani
Date: 2/23/2007 12:01:25 PM
Comment:
This Article is very helpful and contains solution to practical issues.Thanks to Azam Sharp
Subject: Thank you
Name: tkinugaw
Date: 4/15/2007 10:24:11 PM
Comment:
Thank you very much. It is just I want.
Subject: Value of a checkbox that is bound
Name: Mike
Date: 4/18/2007 3:47:37 PM
Comment:
Hi,

I've been trying to come up with a way to read the value of a checkbox that is declared so it displays a field called "Stndby" from an Access table:




Is there any simple way to get this true / false value?

Thanks for any help.
Subject: gridview
Name: Jipson
Date: 5/15/2007 2:24:46 AM
Comment:
It was very helpful to me.
Subject: Question
Name: Leonardo
Date: 5/22/2007 3:44:01 PM
Comment:
What about if the control is a Image and not a checkbox?
Subject: retreaving information from a hidden column
Name: German
Date: 6/1/2007 1:19:35 AM
Comment:
Hi! I used your method (using control label) and it works! thank you!
Subject: ITs very good
Name: Subramanyam
Date: 6/2/2007 11:09:14 PM
Comment:
this is very good to using for gridview thanks for your artical
Subject: good job
Name: Rajeev Patil
Date: 6/15/2007 3:44:54 AM
Comment:
thanks
Subject: Thanks!!
Name: aXEL
Date: 6/19/2007 1:27:46 PM
Comment:
You are so smart!!!!
Thanks!!!

Axeliux! :)
Subject: Thanks!
Name: MacK
Date: 6/21/2007 9:39:10 AM
Comment:
Thanks! you saved me a lot of time
Subject: Thank Q
Name: hari krishna
Date: 7/12/2007 10:27:38 PM
Comment:
Thank U sir/madam, ur informatin is very useful to me.

You need to append morethan one datakeynames accessing also. It's my request.
Subject: Awesome!!!
Name: DannyC
Date: 7/21/2007 11:48:30 AM
Comment:
I was going crazy searching for an answer until I found this article. Great job!!
Subject: Thanks
Name: David Dickson
Date: 7/24/2007 12:39:06 AM
Comment:
A very useful, easy to follow article
Brilliant !
Subject: Acess GridView Invisible columns
Name: kiruthika
Date: 7/25/2007 2:22:54 AM
Comment:
I'm really thank for who prepared this article.It's very useful and it'll help in future those who are like me
Subject: RE: Access Invisible Columns
Name: AzamSharp
Date: 7/25/2007 8:33:12 AM
Comment:
Hi,

I am really glad that you liked the article.
Subject: WOW !!!
Name: Adrian
Date: 9/26/2007 10:42:50 AM
Comment:
Thank you, This helps me a lot
Subject: Too good
Name: Anirudha
Date: 10/12/2007 10:06:31 AM
Comment:
Hi,
Excellent article. Thanks a lot.
Subject: excellent
Name: sampath
Date: 10/24/2007 8:31:32 AM
Comment:
Excellent.....I have solve my problem..Thanks.
Subject: gridview
Name: naren
Date: 10/26/2007 3:39:17 AM
Comment:
this article was nice
it was too easy to find out how hidden columns can be accessed .
Subject: HTML Control Access
Name: vatsal
Date: 11/5/2007 6:55:37 AM
Comment:
it is good. however instead of server control , if I have put HTML control how can I access it ?
Subject: RE: HTML Control Access
Name: AzamSharp
Date: 11/6/2007 9:42:54 AM
Comment:
Hi vatsal,

You will have to add the runat="server" attribute to the HTML control in order to access the control on the server side.
Subject: Multi-page Grid
Name: Batnik
Date: 11/11/2007 12:57:21 PM
Comment:
Yes, it works but only for shown rows on a page. If you have many pages and you're on Page X you will not be able to get the value from Page Y. Will you?
Any thoughts?
Thanks
Subject: RE: Multi-page Grid
Name: AzamSharp
Date: 11/13/2007 8:46:31 AM
Comment:
Hi Batnik,

You can only access the controls what are visible on the GridView page. Why do you want to access the controls that are on different GridView pages? What is the purpose?

Subject: Awesome
Name: mike wong
Date: 11/14/2007 11:27:04 PM
Comment:
This article is very useful
Thanks man!..
keep it up!!

Subject: RE: Awesome
Name: AzamSharp
Date: 11/15/2007 5:33:14 AM
Comment:
Hi Mike Wong, I am glad that you liked the article. Please visit www.koffeekoder.com for a new version on this website.
Subject: Gridview query
Name: Bijuraj
Date: 12/5/2007 3:02:04 AM
Comment:
Hi friends,

i want to read a value from invisible column in gridview.
Suppose i have three bound columns 1. Id 2.Button 3.name, in this the Id should be invisible, when i click the button i should be able get the correspoding id of a name in that row. can anybody help?
Thanks
Subject: RE: Read the article carefully!
Name: AzamSharp
Date: 12/5/2007 1:49:47 PM
Comment:
Hi Bijuraj,

Read the article carefully! I have explained how to access invisible columns.
Subject: Thanku
Name: vinay
Date: 1/29/2008 4:18:02 AM
Comment:
Thanks
your article is very good
Subject: good quick tutorial
Name: jason
Date: 2/28/2008 12:25:41 PM
Comment:
it was helpful and quick to find a solution using this tutorial. i woke up and smelled the koffee
Subject: gOOD ARTICLE
Name: suvasish
Date: 3/4/2008 2:01:56 AM
Comment:
good article
Subject: Hi
Name: aaryan
Date: 3/25/2008 3:03:03 AM
Comment:
Excellent Articles....
Subject: thnx
Name: mittal
Date: 4/1/2008 5:59:06 AM
Comment:
thnk u so much.its very help
Subject: Thanks, very Clear
Name: Carlos
Date: 4/2/2008 10:47:14 AM
Comment:
Excellent article, very useful!
Subject: RE: Thanks very clear
Name: AzamSharp
Date: 4/3/2008 2:30:20 PM
Comment:
Hi Carlos, I am glad you liked the article.
Subject: be
Name: Masya
Date: 5/3/2008 11:34:23 AM
Comment:
Great article! I've been looking for the way of reading from invisible fields! Thanks a lot!
Subject: Thanks
Name: Andy
Date: 5/12/2008 6:00:49 AM
Comment:
Thank you it was helpful
Subject: Thanks
Name: Emmanuel Toledo
Date: 5/13/2008 11:24:29 AM
Comment:
Your article saved me from a big headache, thank you very much keep up the good work!!
Subject: RE: Thanks
Name: AzamSharp
Date: 5/17/2008 8:53:00 PM
Comment:
Hi Emmanuel Toledo, I am glad you liked the article! Also, check out www.refactorcode.com.
Subject: Thanks!
Name: Patricia
Date: 5/31/2008 2:26:18 PM
Comment:
Thank youuuu! :)
Subject: Excellent!
Name: Nij
Date: 6/23/2008 4:18:43 PM
Comment:
Excellent help, Thank you! I needed to know about the template field to access 'invisible' data. Much appreciated...
Subject: gridview events
Name: dheeraj
Date: 7/17/2008 1:49:53 AM
Comment:
very much helpful
Subject: how to find button controls inside the gridview
Name: senthil
Date: 7/21/2008 5:30:37 AM
Comment:
i want the details about button controls inside the gridview,i want write the code depending upon the button click inside the gridview,pls send me details regarding this issue



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008