Click here to Skip to main content
15,887,746 members
Articles / Web Development / HTML

How To Get Multiple Selected Rows From GridView In ASP.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
19 Oct 2014CPOL1 min read 53.7K   9   3
How to get multiple selected rows from GridView in ASP.NET

In my last post, I explained how easily we can add a CheckBox column to a GridView in ASP.NET. Having a CheckBox column in GridView, now we can select multiple records from the GridView. We can now write some piece of code with the help of which we can perform multiple actions on the selected rows, like deleting the records, updating records, etc. Continuing to the last post, today we will see how we can use these checkboxes to get selected rows from the GridView in a PostBack in ASP.NET and display them to the user for deletion.

We will use the same example we used in our last example where we have populated a GridView with 10 students. We will add a Delete button at the bottom of the GridView and handle the Button’s click event handler to get all selected rows of the GridView and display them to the user for confirmation.

Below is the complete code for ASPX page:

ASP.NET
<asp:label ID="lblMsg" runat="server"
Visible="false"
Text="The below records are selected for deletion"></asp:label>
<asp:gridview ID="grdData" runat="server"
AutoGenerateColumns="False" CellPadding="4"
CssClass="grdData"
    ForeColor="#333333" GridLines="None"
    Width="200" AllowPaging="True"
    OnPageIndexChanging="grdData_PageIndexChanging">
    <alternatingrowstyle BackColor="White"
    ForeColor="#284775"></alternatingrowstyle>
    <columns>
        <asp:templatefield HeaderText="Select">
            <itemtemplate>
                <asp:checkbox ID="cbSelect"
                CssClass="gridCB" runat="server"></asp:checkbox>
            </itemtemplate>
        </asp:templatefield>
        <asp:boundfield DataField="ID"
        HeaderText="ID"></asp:boundfield>
        <asp:boundfield DataField="Name"
        HeaderText="Name"></asp:boundfield>
    </columns>
    <editrowstyle BackColor="#999999"></editrowstyle>
    <footerstyle BackColor="#5D7B9D"
    Font-Bold="True" ForeColor="White"></footerstyle>
    <headerstyle BackColor="#5D7B9D"
    Font-Bold="True" ForeColor="White"></headerstyle>
    <pagerstyle BackColor="#284775"
    ForeColor="White" HorizontalAlign="Center"></pagerstyle>
    <rowstyle BackColor="#F7F6F3"
    ForeColor="#333333"></rowstyle>
    <selectedrowstyle BackColor="#E2DED6"
    Font-Bold="True"
    ForeColor="#333333"></selectedrowstyle>
    <sortedascendingcellstyle
    BackColor="#E9E7E2"></sortedascendingcellstyle>
    <sortedascendingheaderstyle
    BackColor="#506C8C"></sortedascendingheaderstyle>
    <sorteddescendingcellstyle
    BackColor="#FFFDF8"></sorteddescendingcellstyle>
    <sorteddescendingheaderstyle
    BackColor="#6F8DAE"></sorteddescendingheaderstyle>
</asp:gridview>
<asp:button ID="btnDelete" runat="server"
Text="Delete" OnClick="btnDelete_Click"></asp:button>

Below is the code written for the Delete button’s click handler. To understand the data bound to the GridView, please refer to my previous article.

JavaScript
protected void btnDelete_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Id");
    dt.Columns.Add("Name");
    foreach (GridViewRow item in grdData.Rows)
    {
        if ((item.Cells[0].FindControl("cbSelect") as CheckBox).Checked)
        {
            DataRow dr = dt.NewRow();
            dr["Id"] = item.Cells[1].Text;
            dr["Name"] = item.Cells[2].Text;
            dt.Rows.Add(dr);
        }
    }
    lblMsg.Visible = true;
    grdData.DataSource = dt;
    grdData.DataBind();
}

Below are 2 screenshots showing the rows selected on 1st screen and then the selected rows in 2nd screen.

get-multiple-selected-gridview-1

get-multiple-selected-gridview-2

Hope you like this! Keep learning and sharing! Cheers!

License

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


Written By
Founder Rebin Infotech
India India
A passionate developer with over 10 years of experience and building my software company code by code. Experience withMS Technologies like .Net | MVC | Xamarin | Sharepoint | MS Project Server and PhP along with open source CMS Systems like Wordpress/DotNetNuke etc.

Love to debug problems and solve them. I love writing articles on my website in my spare time. Please visit my Website for more details and subscribe to get technology related tips/tricks. #SOreadytohelp

Comments and Discussions

 
QuestionThis is nearly perfect :) Pin
Member 1578219228-Sep-22 16:47
Member 1578219228-Sep-22 16:47 
GeneralMy Vote 5 Pin
Shemeemsha (ഷെമീംഷ)29-Oct-14 20:09
Shemeemsha (ഷെമീംഷ)29-Oct-14 20:09 
Good One Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup: Thumbs Up | :thumbsup:
GeneralRe: My Vote 5 Pin
Nitesh Kejriwal30-Oct-14 17:33
professionalNitesh Kejriwal30-Oct-14 17:33 

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.