Click here to Skip to main content
13,086,120 members (82,594 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 11 Sep 2012

Select Multiple RowS in DataGrid with Checkbox

, 28 Jul 2013
Rate this:
Please Sign up or sign in to vote.
Users can select a row with the help of a checkbox and retrieve information on button click.


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.


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:DataGrid runat="server" CellPadding="4" 

        ForeColor="#333333" GridLines="None"

        AutoGenerateColumns="False" Width="597px" 

        ID="gdCartHistory" OnEditCommand="gdCartHistory_EditCommand"

        Font-Names="Verdana" Font-Size="Small" 

    <AlternatingItemStyle BackColor="White" />
        <asp:BoundColumn DataField="model_name" 

        HeaderText="Model Name"></asp:BoundColumn>
        <asp:BoundColumn DataField="prod_description" 

        HeaderText="Product Description"></asp:BoundColumn>
        <asp:BoundColumn DataField="cost" 

        <asp:BoundColumn DataField="quantity" 

        <asp:BoundColumn DataField="total" 

        HeaderText="Total Amount"></asp:BoundColumn>
        <asp:EditCommandColumn CancelText="Cancel" 

        EditText="Edit" HeaderText="Action" 

        <asp:TemplateColumn HeaderText="Remove">
                <asp:LinkButton ID="lnkedit" runat="server" 

                CommandName="Delete" ForeColor="
        <asp:TemplateColumn HeaderText="Select">
                <asp:CheckBox ID="cbSelected" runat="server" />
        <asp:TemplateColumn HeaderText="Key" Visible="False">
                <asp:Label runat="server" ID="key" 

                Text='<% #Eval("cart_id") %>'></asp:Label>
    <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:Button ID="btbuy" 

   runat="server" Text="Buy Now" BackColor="#507CD1" 

   Font-Bold="True" Font-Names="Verdana" 

   ForeColor="White" OnClick="btbuy_Click" /> 


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.


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


About the Author

Vaibhav Bodake
Software Developer Alitesoft
India India
Software Engineer with year of successful records serving small and mid scale .NET applications. Have a wide range of experience working in domestic client environment. I always love to learn new technologies and share with others.

You may also be interested in...


Comments and Discussions

QuestionMy vote of 4 Pin
blachsmith29-Jul-13 15:28
memberblachsmith29-Jul-13 15:28 
GeneralMy vote of 5 Pin
Anurag Sarkar28-Jul-13 6:37
professionalAnurag Sarkar28-Jul-13 6:37 
GeneralMy vote of 3 Pin
Vaibhav Bodake27-Jul-13 20:35
memberVaibhav Bodake27-Jul-13 20:35 
SuggestionAlternative Pin
DiegoDuarteG17-Sep-12 10:01
memberDiegoDuarteG17-Sep-12 10:01 
GeneralRe: Alternative Pin
Vaibhav Bodake26-May-13 20:43
memberVaibhav Bodake26-May-13 20:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 28 Jul 2013
Article Copyright 2012 by Vaibhav Bodake
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid