I was using a bound
to display a large number of records, with varying data widths in each column.
Initially, I was autosizing all the columns on data load, but with thousands of results, this was causing a lag when the data was loaded.
I then switched to autosizing only when the user scrolled the data, and on the displayed data, however this had the undersirable effect of making the columns all jump around (grow/shrink) as the user scrolled up and down the data.
I decided the best approach was a growing only column width, so as each column encountered longer data, the columns would be resized to fit the new data, but would not shrink again.
This worked great, improved the data load time, and resulted in a happy balance between visuals, usability and data load.
To achieve this, the
in the design environment, this caters for the first instance the data is bound to the
. Then, in the scroll event, turn this setting off as it is no longer required, and run the following snippet.
is the name of my
Private Sub DataResults_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataResults.Scroll
DataResults.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
For Each column As DataGridViewColumn In DataResults.Columns
If column.GetPreferredWidth(DataGridViewAutoSizeColumnMode.DisplayedCells, True) > column.Width Then
column.Width = column.GetPreferredWidth(DataGridViewAutoSizeColumnMode.DisplayedCells, True)