Click here to Skip to main content
15,891,033 members
Articles / Web Development / ASP.NET
Tip/Trick

Select Multiple RowS in DataGrid with Checkbox

Rate me:
Please Sign up or sign in to vote.
4.75/5 (6 votes)
28 Jul 2013CPOL1 min read 40.9K   13   5
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)


Written By
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.

Comments and Discussions

 
QuestionMy vote of 4 Pin
blachsmith29-Jul-13 15:28
blachsmith29-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
professionalVaibhav Bodake27-Jul-13 20:35 
SuggestionAlternative Pin
DiegoDuarteG17-Sep-12 10:01
DiegoDuarteG17-Sep-12 10:01 
GeneralRe: Alternative Pin
Vaibhav Bodake26-May-13 20:43
professionalVaibhav 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.