Accessing Invisible Columns of GridView Control
By AzamSharp
Views: 12089

Introduction:

When you display data in a GridView you should always be very careful not to show the primary key of the table in GridView. In Datagrid we made these columns invisible and hence the client was not able to see them. Unfortunately all invisible columns of the Datagrid were stored in the ViewState and hence opened a huge security hole. GridView does not allow client to see the invisible columns and does not store them in the ViewState object. In this article we will see that how we can access the invisible columns of the GridView control.

Accessing Invisible Columns:

The first thing you need to do is to set the DataKeyNames property to the primary key of your table which in my case is "PersonID".

<asp:GridView ID="gvMaster" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="PersonID" DataSourceID="mySource" ForeColor="#333333" GridLines="None"
Width="266px" OnSelectedIndexChanged="gvMaster_SelectedIndexChanged">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="PersonID" HeaderText="PersonID" InsertVisible="False"
ReadOnly="True" SortExpression="PersonID" Visible="False" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>

My GridView control consists of three columns (leave out the select column) namely PersonID, Name and a checkbox column. PersonID column is the primary key and it has been marked as invisible column. We also have a simple Button control on the web form which when pressed iterates through the GridView control and prints out the primary keys of the columns that have been checked using the CheckBox control.

If you are interested in finding out how to select single as well as multiple checkboxes in the GridView control than please take a look at my article Selecting multiple checkboxes inside the GridView control.

Let's see the Button Click event code:

protected void Button1_Click(object sender, EventArgs e)

{

string str = String.Empty;

int rowNo = 0;

foreach (GridViewRow row in gvMaster.Rows)

{

bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;

if (isChecked)

{

// Gets the name of the primary key column our primary key is the PersonID

string primaryKey = gvMaster.DataKeyNames[0];

int personID = (int) gvMaster.DataKeys[rowNo].Value;

Response.Write(personID);

}

// increment the row count

rowNo++;

}

Response.Write(str);

}

 

Most of the code is old which I just copied and pasted from my previous GridView articles. The important code is in bold characters. We simply retrieve the name of the key from the GridView using the DataKeyNames property and then uses the DataKeys property to get the value of the key. The integer variable rowNo is a counter that displays the values of the primary keys of checked rows. And finally we increment the counter, rowNo and write the values of the primary keys on the page using the Response.Write method.

I hope you liked the article, happy coding!

By AzamSharp


Enter Comment/Feedback
  •  
  •  
  •  
  •  
  •  

Comments/Feedbacks
Subject: Thanks
Name: Edu
Date: 2/2/2007 8:03:35 PM
Comment:
That helped me!
Subject: thanks
Name: dilip
Date: 4/24/2007 5:32:08 AM
Comment:
thanks alot
it was acording to my requirement.
any updrades send to mail
Subject: It works to me
Name: seev
Date: 6/18/2007 7:17:22 PM
Comment:
Thanks it helped me out...
Subject: coding
Name: ritha
Date: 6/22/2007 5:07:47 AM
Comment:
good
Subject: Cannot retrieve values from the invisible column.
Name: Harry Lam
Date: 8/27/2007 10:11:39 AM
Comment:
Hello there,

I find out that it is not possible to retrieve that values from an invisible columns in a gridview. I have tried and changed your code many times, but I still cannot get these values, they just return an empty string or null. When I set that column to 'visible', it works perfectly. But when it is invisible, it returns nothing.

Hope anyone can help me, thanks a lot!
Subject: Great Article
Name: Steven McCracken
Date: 9/13/2007 2:44:32 PM
Comment:
Your article (Accessing Invisible Columns of GridView Control) was just what I needed to see. I’m upgrading from the DataGrid control and I found that I can’t retrieve data from invisible columns in a GridView the way I could with a DataGrid. I had the same problems Harry Lam did. The only minor difference between your sample code and my own solution is that I’m using VB.Net. But the structure and function of the control is the same.
Subject: RE: Great Article
Name: AzamSharp
Date: 9/14/2007 2:10:42 PM
Comment:
Hi Steven McCracken,

I am glad that you found the article useful.
Subject: Good
Name: Mohd Islam
Date: 10/17/2007 11:09:04 PM
Comment:
Good Material
Subject: test
Name: Harish
Date: 12/13/2007 3:14:04 PM
Comment:
Thanks a lot for your suggestion. it helped me and saved lot of time for me.



Join WebHost4Life.com






Copyright GridViewGuy 2007-2008