Click here to Skip to main content
16,007,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


I am using a DataGridview control in windows application ,now i have select one row then click the delete button the row is deleted.But i want delete multiple rows.
Posted
Comments
Prasad_Kulkarni 20-Jan-12 7:45am    
You want to use check box to delete multiple rows?

Hi,
XML
<asp:SqlDataSource ID="SqlDataSource1" Runat="server"
    SelectCommand="SELECT EmployeeID, LastName, City FROM Employees"
    DeleteCommand="DELETE FROM Employees WHERE [EmployeeID] = @EmployeeID"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" >

    <DeleteParameters>
        <asp:Parameter Name="EmployeeID" />
    </DeleteParameters>
</asp:SqlDataSource>

XML
<asp:TemplateField>
    <ItemTemplate>
        <asp:CheckBox ID="chkRows" runat="server"/>
    </ItemTemplate>
</asp:TemplateField>

This will add a checkbox to each row in the grid.
XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="cbRows" runat="server"/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False" ReadOnly="True" SortExpression="EmployeeID" />
        <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
        <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
    </Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" Runat="server"
    SelectCommand="SELECT EmployeeID, LastName, City FROM Employees"
    DeleteCommand="DELETE FROM Employees WHERE [EmployeeID] = @EmployeeID"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" >

    <DeleteParameters>
        <asp:Parameter Name="EmployeeID" />
    </DeleteParameters>
</asp:SqlDataSource>

<asp:Button ID="btnMultipleRowDelete"  OnClick="btnMultipleRowDelete_Click" runat="server"  Text="Delete Rows" />


C#
protected void btnMultipleRowDelete_Click(object sender, EventArgs e)
{
    // Looping through all the rows in the GridView
    foreach (GridViewRow row in GridView1.Rows)
    {
        CheckBox checkbox = (CheckBox)row.FindControl("cbRows");

        //Check if the checkbox is checked.
        //value in the HtmlInputCheckBox's Value property is set as the

        //value of the delete command's parameter.
        if (checkbox.Checked)
        {
            // Retreive the Employee ID
            int employeeID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);

            // Pass the value of the selected Employye ID to the Delete //command.
            SqlDataSource1.DeleteParameters["EmployeeID"].DefaultValue = employeeID.ToString();
            SqlDataSource1.Delete();
        }
    }
}

Hope this will help.
 
Share this answer
 
Comments
RDBurmon 20-Jan-12 8:20am    
My +5
Prasad_Kulkarni 23-Jan-12 1:40am    
Thank you
try this
CP Tips

To delete,you will select rows and press Delete key and click button, rows will be deleted.(means database updated)
 
Share this answer
 
v2
Select multiple rows (make sure the 'multiselect' property is set to true) and click delete.
or
program it yourself e.g. using a ContextMenuStrip:
C#
foreach (DataGridViewRow row in DataGridView1.SelectedRows) {
	DataGridView1.Rows.Remove(DataGridView1.Rows(row.Index));
}
 
Share this answer
 
v3
Hello,

1) put the check box in front of each row, check it, and use loop on button click to delete the record

and you achieved you desired...
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900