Friday 17 April 2015

ASP.Net GridView Hidden Column: Get Hidden Column values using C# and VB.Net



Here Mudassar Ahmed Khan has explained the concept of Hidden Columns in ASP.Net GridView i.e. how to hide a particular column value in GridView and then use it processing in C# and VB.Net.
The database columns can be hidden by binding it to DataKeyNames (DataKeys) property of the ASP.Net GridView.

In this article I will explain the concept of Hidden Columns in ASP.Net GridView i.e. how to hide a particular column value in GridView and then use it processing in C# and VB.Net.
The database columns can be hidden by binding it to DataKeyNames (DataKeys) property of the ASP.Net GridView.

What are DataKeyNames and DataKeys?
DataKeyNames is the property of GridView which allows us to set the names of the Column Fields that we want to use in code but do not want to display it. Example Primary Keys, ID fields, etc.
The values Column Fields which are set in DataKeyNames are available in code in DataKeys object which saves it in an Array called as DataKeyArray.

Using DataKeyNames and DataKeys in GridView
Using DataKeyNames and DataKeys is fairly simple, you just need to set the name of the Column in DataKeyNames property as shown below. Here CustomerId is the name of the Column.
DataKeyNames="CustomerId"

And then in code access it using the RowIndex of the GridView Row to get the value of the Column for that particular Row.
int id = Convert.ToInt32(GridView1.DataKeys[rowIndex].Values[0]);

Below is an example where I will explain how to use DataKeyNames and DataKeys in GridView control.
HTML Markup
The below GridView has the DataKeyNames set and also has a Button which will be used to get the DataKeys value for that row.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="Id">
<Columns>
    <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
    <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="100" />
    <asp:TemplateField>
        <ItemTemplate>
            <asp:Button Text="Get Value" runat="server" OnClick="GridView_Button_Click" />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>


Namespaces
You will need to import the following namespaces.
C#
using System.Data;

VB.Net
Imports System.Data


Binding the GridView
The GridView is populated using some dummy records using DataTable.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Id"), new DataColumn("Group"), new DataColumn("Name"), newDataColumn("Country") });
        dt.Rows.Add(1, "A", "John Hammond", "United States");
        dt.Rows.Add(2, "B", "Mudassar Khan", "India");
        dt.Rows.Add(3, "A", "Suzanne Mathews", "France");
        dt.Rows.Add(4, "B", "Robert Schidner", "Russia");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(3) {New DataColumn("Id"), New DataColumn("Group"), New DataColumn("Name"), New DataColumn("Country")})
        dt.Rows.Add(1, "A", "John Hammond", "United States")
        dt.Rows.Add(2, "B", "Mudassar Khan", "India")
        dt.Rows.Add(3, "A", "Suzanne Mathews", "France")
        dt.Rows.Add(4, "B", "Robert Schidner", "Russia")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub


Getting the DataKeys value for a particular GridView Row
Inside the Button click event handler, first the GridView Row is determined using the NamingContainer property and then finally the Row Index is determined.
Using the Row Index, the DataKeys array is accessed and the value of the Column is fetched.
C#
protected void GridView_Button_Click(object sender, EventArgs e)
{
    //Determine the RowIndex of the Row whose Button was clicked.
    int rowIndex = ((sender as Button).NamingContainer as GridViewRow).RowIndex;

    //Get the value of column from the DataKeys using the RowIndex.
    int id = Convert.ToInt32(GridView1.DataKeys[rowIndex].Values[0]);
}

VB.Net
Protected Sub GridView_Button_Click(sender As Object, e As EventArgs)
    'Determine the RowIndex of the Row whose Button was clicked.
    Dim rowIndex As Integer = TryCast(TryCast(sender, Button).NamingContainer, GridViewRow).RowIndex

    'Get the value of column from the DataKeys using the RowIndex.
    Dim id As Integer = Convert.ToInt32(GridView1.DataKeys(rowIndex).Values(0))
End Sub

No comments:

Post a Comment