1

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:

 '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()
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Topher
  • 21
  • 1
  • 1
  • 7
  • Is there something else going on with the gridview that you're not showing. For instance, are you re-quering a data source? This may slow down your response but just hiding columns should not. – jason Mar 04 '13 at 19:00
  • Why are you hiding all the columns? Can't you hide the grid or unbind the data source? – Esselans Mar 05 '13 at 01:38

2 Answers2

6

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

Trevor
  • 7,777
  • 6
  • 31
  • 50
1

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.

Tim F.
  • 268
  • 2
  • 8