vb.net 在 DataGridView 中隐藏列非常慢

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15205937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:36:23  来源:igfitidea点击:

vb.net hiding Columns in DataGridView is very slow

vb.netvisual-studio-2010.net-4.0datagridviewdoublebuffered

提问by Topher

trying to hide 44 columns of a DataGridView take 44 seconds on Windows 7 machine. How can I speed this up? I used the following code:

在 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()

回答by ???ěxě?

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...

将您的循环更改为此,因为这将遍历列并使它们不可见......为了我的测试只是为了确保,我添加了 250 列并在大约一秒钟内使用此循环将它们全部隐藏......

 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...

至于您对 datagridview 进行双缓冲,对表单进行双缓冲,因为它会减少该表单上发生的任何闪烁。这里有两个选项:1 - 在属性窗口中为您的表单设置双缓冲区或 2 - 初始化另一个子程序以对其进行双缓冲...

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
    
  • 1 把它放在表格的最顶部......

    Imports System.Reflection
    
  • 2 将此添加到您的表单加载...

    BufferMethod.DoubleBuffered(DataGridView1, True)
    
  • 3 在其他课程的最后(在 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

程序员先生

回答by Tim F.

The autosizemode property of a column, when set to automatically configure according to contents (like displayedcells) can slow the whole grid down. It seems to redraw "internally". I solved my grid problems by using those types on small grids only and very sparingly for others. Took me a while to figure this was the problem because there is no external draw/event happening it just appears very slow.

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