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