Deleting multiple rows in datagridview

how to delete multiple rows in datagridview using checkbox in c#
how to delete multiple selected rows in datagridview in vb net
c# datagridview select multiple rows programmatically
delete datagridview row programmatically c#
delete selected row in datagridview c#
how to delete record from database using datagridview in c#
how to get multiple selected rows in datagridview c#

I need to delete top 1000 rows in a datagridview.

delegate void DeleteRowsCallback(); 
    private static void Delete1KRows()
    {
        JSONTest form = (JSONTest)Application.OpenForms["JSONTest"];
        if (form.GridTestReport.InvokeRequired)
        {
            DeleteRowsCallback d = new DeleteRowsCallback(Delete1KRows);
            form.GridTestReport.Invoke(d);

        }
        else
        {
            for (int i = 0; i < 1000; i++ )
            {
                form.GridTestReport.Rows.RemoveAt(0);

            }
        }

    }

This deletes the rows but takes lot of time. Also the UI is non-responsive when the delete is in progress. Any better way to delete multiple rows.

Thanks


As you are not using any binding or virtual mode, everytime you remove a row, the entire grid is refreshed. It would be painfully slow.

As you are dealing with large amount of data, use Virtual Mode to efficietly update the grid.

See this MSDN link for more information. There is a Walkhrough available. It takes little time to setup but once it is done it would much faster ane makes life easier.

Also see Best Practices for Scaling the Windows Forms DataGridView Control to get the maximum out DataGridView.

[Solved] How To Delete Multiple Rows, Hi If you are looking a way in C# the below solution is too much easy solution: Hide Copy Code. private void buttonRemove_Click(object  eg: I want to delete row 1 and 2 in datagrid, I stored their rowIndex in datagrid. after row 1 is deleted in datagrid, data in row 2 will SHIFT UP to row 1, and data in row 3 will SHIFT UP to row 2, because you are using the datagrid rowIndex to locate what data to delete, therefore, result: data1 and data3 will be deleted finally.


There is my working solution. First delete the rows in database and then refill datagridview. NOTE: I use database access (oledbcommand, oledbconnection, etc...), change for your connection.

try
{
    //connectDB is the procedure to connect to database:
    connectDB();
    //this delete first the selected rows in database, and then update datagrid
    int countRows = dataGridView1.SelectedRows.Count;
    for (int i = 0; i < countRows; i++)
    {
        int currentRow = dataGridView1.SelectedRows[0].Index;
        //con.oledbcon is the conecction is the OleDbConnection procedure, miTable = name of your table
        OleDbCommand com = new OleDbCommand("DELETE FROM miTable WHERE Id=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString(), con.oledbcon);
        // delete from database
        com.ExecuteNonQuery();
        // THIS IS THE KEY!!! deselect the last row selected to be able to delete the next row in the next loop
        dataGridView1.Rows[currentRow].Selected = false;
    }
    // all OK
    MessageBox.Show("Rows deleted!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
    //in case of error
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
    //disconnectDB is the procedure to disconnect to database:
    disconnectDB();
    // and finally, refill datagrid again
    loaddatagrid();
}

C# Tutorial, Delete multiple rows from DataGridView based on CheckBox selection in C#. The C# Basics Duration: 10:01 Posted: Jul 26, 2016 To Delete Multiple Rows call following sub on Event Handler or Button Click:- Private Sub ClearDataGrid() For Each row As DataGridViewRow In DataGridView1.Rows


When DataGridView activities run slowly in my projects, it is usually because one or more columns are configured to auto-size, and this auto-sizing happens over the whole grid as each row is deleted. To prevent it, I use code similar to the following:

Before the mass deletion:

var autoSizeModes = grid.Columns.Cast<DataGridViewColumn>).ToDictionary(col => col, col => col.AutoSizeMode);
foreach (var column in autoSizeModes.Keys)
    column.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;

After the mass deletion:

foreach (var column in autoSizeModes)
    column.Key.AutoSizeMode = column.Value;

Delete Multiple Records From A DataGridView Using CheckBox In , Delete Multiple Records From A DataGridView Using CheckBox In Windows Form. public partial class Form1: Form. { SqlConnection con = new SqlConnection("Data Source=DEBENDRA;Initial Catalog=Students;User ID=sa;Password=123"); int Index = 0; SqlDataAdapter da; public Form1() { InitializeComponent(); Deleting multiple rows from DataGridView control on Button click When the Delete button is clicked the following event handler is executed. It first gets the checked (selected) rows from the DataGridView using LINQ query and then displays a confirmation Message Box.


How To Delete Multiple Selected Rows of Datagridview ?, How To Delete Multiple Selected Rows of Datagridview ? SqlCommand delcmd = new SqlCommand(); if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != { delcmd.CommandText = "DELETE FROM StudentRegistration WHERE Rollno=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + ""; //con.Open(); C#, JAVA,PHP, Programming ,Source Code dataGridView datatable Delete DataGridView Selected Row Using VB.NET Remove DataGridView Selected Row In VB.NET VB.NET vb.net dataGridView vb.net datatable VB.NET - How To Delete A DataGridView Selected Row Using VB.NET


Deleting multiple selected rows in a datagridview by only a click , With this routine we can delete multiple selected rows with only a clic maybe after a search in the table. We have to clic on the tooths in the  The FullRowSelect and RowHeaderSelect modes allow users to delete rows by selecting them and pressing the DELETE key. Users can delete rows only when the current cell is not in edit mode, the AllowUserToDeleteRows property is set to true, and the underlying data source supports user-driven row deletion.


Delete multiple rows from DataGridView based on CheckBox , Here Mudassar Ahmed Khan has explained how to delete (remove) multiple selected rows with confirmation from DataGridView based on  You simply bind your data to the grid, i.e. assign your DataTable to the DataSource property of the grid, and then the user can select multiple grid rows and press Delete to automatically the delete the corresponding records.