Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Select Multiple RowS in DataGrid with Checkbox

4.75/5 (6 votes)
28 Jul 2013CPOL1 min read 41.6K  
Users can select a row with the help of a checkbox and retrieve information on button click.

Image 1

Introduction 

It is difficult to retrieve data from a selected row in a DataGrid to another control like a button. To solve this problem, this code helps the user to retrieve data from the selected row of a DataGrid to other control in ASP.NET.

Background

DataGrid is a control in ASP.NET used to display data. To retrieve data from a DataGrid, there are inbuilt functions of DataGrid like the select command. But if the user wants to access data in other controls, then this code is helpful.

Using the Code

For using this code, the user has to have a DataGrid with multiple columns. In that DataGrid for multiple selection of rows, take a template column containing a CheckBox. After that, take a hidden template column containing a Label control with the primary key. All this can be done with the DataGrid property builder and in that the user can create a column and bind data to the DataGrid. The user can change the format of the DataGrid with auto format. After that, take a button outside the DataGrid to access data from the DataGrid. In the button click event, write the following code to access items in the DataGrid which will help users to retrieve the correct data row like this:

ASP.NET
<asp:DataGrid runat="server" CellPadding="4" 
        ForeColor="#333333" GridLines="None"
        AutoGenerateColumns="False" Width="597px" 
        ID="gdCartHistory" OnEditCommand="gdCartHistory_EditCommand"
        Font-Names="Verdana" Font-Size="Small" 
        ondeletecommand="gdCartHistory_DeleteCommand">
    <AlternatingItemStyle BackColor="White" />
    <Columns>
        <asp:BoundColumn DataField="model_name" 
        HeaderText="Model Name"></asp:BoundColumn>
        <asp:BoundColumn DataField="prod_description" 
        HeaderText="Product Description"></asp:BoundColumn>
        <asp:BoundColumn DataField="cost" 
        HeaderText="Cost"></asp:BoundColumn>
        <asp:BoundColumn DataField="quantity" 
        HeaderText="Quantity"></asp:BoundColumn>
        <asp:BoundColumn DataField="total" 
        HeaderText="Total Amount"></asp:BoundColumn>
        <asp:EditCommandColumn CancelText="Cancel" 
        EditText="Edit" HeaderText="Action" 
            UpdateText="Update"></asp:EditCommandColumn>
        <asp:TemplateColumn HeaderText="Remove">
            <ItemTemplate>
                <asp:LinkButton ID="lnkedit" runat="server" 
                CommandName="Delete" ForeColor="
                Black">Remove</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="Select">
            <ItemTemplate>
                <asp:CheckBox ID="cbSelected" runat="server" />
            </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="Key" Visible="False">
            <ItemTemplate>
                <asp:Label runat="server" ID="key" 
                Text='<% #Eval("cart_id") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
    <EditItemStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" 
    Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" 
    Font-Bold="True" ForeColor="White" />
    <ItemStyle BackColor="#EFF3FB" />
    <PagerStyle BackColor="#2461BF" 
    ForeColor="White" HorizontalAlign="Center" />
    <SelectedItemStyle BackColor="#D1DDF1" 
    Font-Bold="True" ForeColor="#333333" />
</asp:DataGrid>
<asp:Button ID="btbuy" 
   runat="server" Text="Buy Now" BackColor="#507CD1" 
   Font-Bold="True" Font-Names="Verdana" 
   ForeColor="White" OnClick="btbuy_Click" /> 

Code-Behind

C#
protected void btbuy_Click(object sender, EventArgs e)
{
    foreach (DataGridItem objItem in gdCartHistory.Items)
    {
        if (objItem.ItemType != ListItemType.Header && objItem.ItemType != 
        ListItemType.Footer && objItem.ItemType != ListItemType.Pager)
        {
             if (((CheckBox)objItem.Cells[0].FindControl("cbSelected")).Checked == true)
             {

                 string key=((Label)objItem.Cells[0].FindControl("key")).Text.ToString();
             }
        }
    }     
}

Points of Interest

With the help of this code, the user can get data from multiple rows from outside the DataGrid. The user can select multiple rows with a check box and retrieve data from outside the DataGrid. It will help user to update data or delete data from the DataGrid.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)