【问题标题】:vb.net hiding Columns in DataGridView is very slowvb.net在DataGridView中隐藏列非常慢
【发布时间】:2013-02-18 19:11:27
【问题描述】:

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

【问题讨论】:

  • 您未显示的网格视图是否还有其他问题。例如,您是否正在重新查询数据源?这可能会减慢您的响应速度,但不应该仅仅隐藏列。
  • 你为什么要隐藏所有的列?不能隐藏网格或者解绑数据源吗?

标签: vb.net visual-studio-2010 .net-4.0 datagridview doublebuffered


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    如果您选择这样做,这将删除所有列...

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

    这是另一种方式......

      DataGridView1.Columns.Clear()
    

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

    这是表单双缓冲的代码...将其直接放在您的类名下...

     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
    

    如果您选择这样做,您可以保留上面的代码,这将有助于您的表单和其上的组件的整体。这是我最喜欢的数据网格视图,以避免任何闪烁,包括滚动条......

    • 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
      

    希望你喜欢!

    问候,

    MrCodexer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-06
      • 2020-08-08
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 2012-07-08
      相关资源
      最近更新 更多