Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to use multiple DataKeyNames in GridView in ASP.NET

Author: Alanna Kremer
Download Source Code : 1394_MultipleDataKeyNames.zip

We will see in this article how we can use DataKeyNames property of GridView for multiple values in ASP.NET.

 

DataKeyNames property of GridView control can be used for multiple values and we can retrieve these values in code behind file. I have used a CheckBox control to select a value and retrieve multiple DataKeyNames on a Button click event.

 

  1. Open Visual Studio 2010
  2. File > New > Web Site
  3. Visual C# or Visual Basic > ASP.NET Empty Web Site > Click Ok
  4. Website > Add New Item > Web Form > Click Add
  5. Now Drag and Drop a GridView, SqlDataSource, Button and three Label controls in Default aspx page
  6. Populate this GridView with data you want. I have populated it with “Products” table of “NORTHWIND” database.
  7. The code in Default.aspx page will be like below. Look at the DataKeyNames property of GridView Control. I have used three values in the Property. 

    <asp:GridView ID="GridView1" AutoGenerateColumns="False"
                DataKeyNames="ProductID, SupplierID, CategoryID" runat="server" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:TemplateField HeaderText="Select">
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
     
            <asp:BoundField DataField="ProductID" HeaderText="ProductID"
                InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
            <asp:BoundField DataField="SupplierID" HeaderText="SupplierID"
                SortExpression="SupplierID" />
            <asp:BoundField DataField="ProductName" HeaderText="ProductName"
                SortExpression="ProductName" />
            <asp:BoundField DataField="CategoryID" HeaderText="CategoryID"
                SortExpression="CategoryID" />
        </Columns>
    </asp:GridView>
    <br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:NORTHWNDConnectionString %>"
        SelectCommand="SELECT TOP 20 [ProductID], [SupplierID], [ProductName], [CategoryID] FROM [Products]">
    </asp:SqlDataSource>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Select and Get Multiple DataKeyNames"
        onclick="Button1_Click" />
    <br />
    <asp:Label ID="lblProductID" runat="server" ></asp:Label>
    <br />
    <asp:Label ID="lblSupplierID" runat="server" ></asp:Label>
    <br />
    <asp:Label ID="lblCategoryID" runat="server" ></asp:Label>

     
  8. Now add code below in button click event in code behind file to retrieve values from DataKeyNames property.

    C#
     
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox checkbox = (CheckBox)row.Cells[0].FindControl("CheckBox1");
     
            if (checkbox.Checked)
            {
                int productID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);
                int supplierID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[1]);
                int categoryID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[2]);
     
                lblProductID.Text = productID.ToString();
                lblSupplierID.Text = supplierID.ToString();
                lblCategoryID.Text = categoryID.ToString();
            }
        }
    }
     
    VB.NET
     
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each row As GridViewRow In GridView1.Rows
            Dim checkbox As CheckBox = DirectCast(row.Cells(0).FindControl("CheckBox1"), CheckBox)
     
            If checkbox.Checked Then
                Dim productID As Integer = Convert.ToInt32(GridView1.DataKeys(row.RowIndex).Values(0))
                Dim supplierID As Integer = Convert.ToInt32(GridView1.DataKeys(row.RowIndex).Values(1))
                Dim categoryID As Integer = Convert.ToInt32(GridView1.DataKeys(row.RowIndex).Values(2))
     
                lblProductID.Text = productID.ToString()
                lblSupplierID.Text = supplierID.ToString()
                lblCategoryID.Text = categoryID.ToString()
            End If
        Next
    End Sub
     
    I have used a “foreach” loop to iterate through all GridView rows. We have to get the CheckBox control to get the checked items of the GridView. If the CheckBox is checked then you can retrieve values in the DataKeyNames property using “DataKeys” array. You can get values by providing index position.
     
  9. Press F5 and see website in browser.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to create RSS feed in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net