Click here to Skip to main content
Click here to Skip to main content

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.

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: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

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)

Share

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.
Follow on   Google+

Comments and Discussions

 
QuestionMy vote of 4 Pinmemberblachsmith29-Jul-13 15:28 
GeneralMy vote of 5 PinprofessionalAnurag Sarkar28-Jul-13 6:37 
GeneralMy vote of 3 PinmemberVaibhav Bodake27-Jul-13 20:35 
SuggestionAlternative PinmemberDiegoDuarteG17-Sep-12 10:01 
GeneralRe: Alternative PinmemberVaibhav Bodake26-May-13 20:43 

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

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

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