1

试图在 Windows 7 机器上隐藏 DataGridView 的 44 列需要 44 秒。我怎样才能加快速度?我使用了以下代码:

 'Turn on DataGridView.DoubleBuffered
 Dim myType As Type = GetType(DataGridView)
 myType.InvokeMember( _
   "DoubleBuffered", _
    BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.SetProperty, _
    Nothing, DataGridView1, New Object() {True})

 'hide the following columns
 Me.SuspendLayout()
 For Each col As DataGridViewColumn In DataGridView1.Columns
    col.Visible = False
 Next
 Me.ResumeLayout()
4

2 回答 2

5

Change your loop to this as this will iterate through the columns and make them not visible... For my test just to make sure, I added 250 columns and hid them all in about a second with this loop...

 For i As Integer = 0 To DataGridView1.ColumnCount - 1
   DataGridView1.Columns(i).Visible = False
 End Sub

This will remove all columns if you choose to do so...

  For i As Integer = 0 To DataGridView1.ColumnCount - 1
   DataGridView1.Columns.Remove(DataGridView1.Columns(0).Name)
  Next

And here is another way...

  DataGridView1.Columns.Clear()

As for you double buffering your datagridview, double buffer the form as it will reduce any flickering that occurs on that form. Here are two options: 1 - set double buffer in the properties window for your form OR 2 - initialize another sub to double buffer it...

Here's the code for double buffering for your form... Put this directly under your class name...

 Public Sub New()
    MyBase.New()

    MyBase.DoubleBuffered = True
    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
 End Sub

You can leave the above code if you choose to do so, this will help overall your form and the components that are sitting on it. Here is my favorite though for a datagridview to avoid any flickering what so ever including the scroll bars...

  • 1 Put this at the very top of your form...

    Imports System.Reflection
    
  • 2 Add this to your form load...

    BufferMethod.DoubleBuffered(DataGridView1, True)
    
  • 3 Drop this new class at the very end of your other class (underneath End Class)

    Public NotInheritable Class BufferMethod
      Public Shared Sub DoubleBuffered(dgView As DataGridView, Setting As Boolean)
          Dim dgvType As Type = dgView.[GetType]()
          Dim propInfo As PropertyInfo = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance Or BindingFlags.NonPublic)
          propInfo.SetValue(dgView, Setting, Nothing)
      End Sub
    End Class
    

Hope You Enjoy!

Regards,

MrCodexer

于 2013-03-05T05:37:31.310 回答
1

列的 autosizemode 属性,当设置为根据内容(如显示的单元格)自动配置时,可能会减慢整个网格的速度。它似乎在“内部”重绘。我通过仅在小网格上使用这些类型解决了我的网格问题,而对其他人则非常谨慎。我花了一段时间才弄清楚这是问题所在,因为没有发生外部绘图/事件,它看起来很慢。

于 2015-12-10T12:43:49.113 回答