Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am developing a desktop application using c#.I want to know how i can delete multiple rows in datagridview using checkbox from databse Ms-Access and Datagridview.

Thanks in Advance
Posted

Try this:

This is how you can get datasource:
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>


This is template field for CheckBox in Grid:
XML
<asp:TemplateField>
    <ItemTemplate>
        <asp:CheckBox ID="chkRows" runat="server"/>
    </ItemTemplate>
</asp:TemplateField>


This will add checkBox in 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" />


Code for deletion of multiple records from Grid:
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 it helps.

Also refer some related links:
Delete Multiple Rows Records Gridview CheckBox Confirmation[^]
Add, Edit, and Delete in DataGridView with Paging[^]

Similar discussion:
delete all selected rows in DataGridView [^]
How to delete multiple rows in a DataGridView[^]
delete multiple rows in datagridview[^]

This one exact matching your requirement:
delete multiple rows in datagridview and access table[^]
 
Share this answer
 
v2
Here is the solution for your question...

C#
if (this.dataGridView1.SelectedRows.Count > 0)
    {
        string queryString = "SELECT ID, Name, LastName FROM Person ";
        foreach (DataGridViewRow dgvrCurrent in dataGridView1.SelectedRows)
        {
             int currentRow = int.Parse(dataGridView1.CurrentCell.RowIndex.ToString());
            try
            {
                string personIDString = dataGridView1[0, currentRow].Value.ToString();
                personIDInt = int.Parse(personIDString);

                string queryDeleteString = "DELETE FROM Person where ID= " + personIDInt + ";";
                OleDbCommand sqlDelete = new OleDbCommand();
                sqlDelete.CommandText = queryDeleteString;
                sqlDelete.Connection = database;
                sqlDelete.ExecuteNonQuery();
             }
            catch (Exception ex) { }
        }

        loadDataGrid(queryString);
    }
 
Share this answer
 
Comments
NeerajRathi 4-Jun-12 2:48am    
Hi Ravi
Thanks for yours solution
Expain the sqldelete.Connection=database; this is give the error

Thanks
Neeraj

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