【问题标题】:Deleting multiple rows in datagridview删除datagridview中的多行
【发布时间】:2014-02-06 05:58:18
【问题描述】:

我需要删除 datagridview 中的前 1000 行。

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);

            }
        }

    }

这会删除行但需要很多时间。当删除正在进行时,UI 也没有响应。删除多行的更好方法。

谢谢

【问题讨论】:

  • 您是否在Virtual 模式下使用DataGridView?您是如何将数据绑定到网格的?
  • 你的 gridview 有数据源吗?你为什么不从它的数据源中删除你想要的所有记录,然后你可以调用 gridview.databind() 来刷新用户界面,这样你就不需要手动删除 gridview 行了。
  • 我没有绑定gridview。我正在调用 form.GridTestReport.Rows.Add()。我没有使用虚拟模式。

标签: c# .net winforms datagridview


【解决方案1】:

当我的项目中 DataGridView 活动运行缓慢时,通常是因为一个或多个列被配置为自动调整大小,并且随着每一行被删除,这种自动调整大小发生在整个网格上。为了防止它,我使用类似于以下的代码:

批量删除前:

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

批量删除后:

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

【讨论】:

    【解决方案2】:

    由于您没有使用任何绑定或虚拟模式,因此每次删除一行时,都会刷新整个网格。这会非常缓慢。

    当您处理大量数据时,请使用Virtual Mode 高效地更新网格。

    有关详细信息,请参阅 this MSDN 链接。有一个Walkhrough 可用。设置所需的时间很短,但一旦完成,它会更快,让生活更轻松。

    另请参阅Best Practices for Scaling the Windows Forms DataGridView Control 以最大限度地利用DataGridView

    【讨论】:

      【解决方案3】:

      有我的工作解决方案。 首先删除数据库中的行,然后重新填充datagridview。 注意:我使用数据库访问(oledbcommand、oledbconnection 等...),更改您的连接。

      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();
      }
      

      【讨论】:

        猜你喜欢
        • 2012-06-21
        • 1970-01-01
        • 1970-01-01
        • 2010-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-25
        相关资源
        最近更新 更多