Welcome

Welcome to CMS n Web. Learn & Study completely Free Articles\Training about Web Designing\Development and CMS.

Search This Web

Monday, July 29, 2013

Select Multiple RowS in DataGrid with Checkbox

By , 28 Jul 2013


Introduction 

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



Background

Datagrid is the control the ASP.NET user can use to display data. To retrieve data from datagrid, there are inbuilt functions of datagrid like select command. But if user wants to access data in other control, then this code is helpful for him.

Using the Code

For using this code, the user has to take one datagrid with multiple columns. In that datagrid for multiple selection of rows, take one template column containing checkbox. After that, take one hidden template column containing label control with primary key. All this can be done with datagrid property builder in that user can create column and bind data to the datagrid. The user can change format ofdatagrid with auto format. After that, take one button outside the datagrid to access data fromdatagrid. In that button click event, write the following code to access the items in datagrid which will help user to retrieve 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 of multiple row data from outside the datagrid. User can select multiple rows with check box and retrieve data from outside the datagrid. It will help user to update data or delete data from datagrid.