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

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!