Click here to Skip to main content
Click here to Skip to main content

DataGridView Column Width Grow Only Sizing

, 20 Jun 2010
Rate this:
Please Sign up or sign in to vote.
A simple way to create a grow only Column Width routine
I was using a bound DataGridView 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 DataGridView has Column AutoSizeColumMode set to DisplayedCells in the design environment, this caters for the first instance the data is bound to the DataGridView. Then, in the scroll event, turn this setting off as it is no longer required, and run the following snippet. DataResults is the name of my DataGridView
 
    Private Sub DataResults_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles DataResults.Scroll
        'Turn off Autosize, only used during the first data load.
        DataResults.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
 
        'Get the preffered width for displayed data against each column and compare to current width
        'Set the new column width only if greater than current
        For Each column As DataGridViewColumn In DataResults.Columns
            If column.GetPreferredWidth(DataGridViewAutoSizeColumnMode.DisplayedCells, True) > column.Width Then
                column.Width = column.GetPreferredWidth(DataGridViewAutoSizeColumnMode.DisplayedCells, True)
            End If
        Next
    End Sub

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

DaveAuld
Engineer
Scotland Scotland
I have been working in the Oil & Gas Industry for over 20 years now.
 
Core Discipline is Instrumentation and Control Systems.
 
Completed Bsc Honours Degree (B29 in Computing) with the Open University in 2012.
 
Currently, Offshore Installation Manager for the Beryl Bravo platform, which is located ~180 miles NE of Aberdeen, Scotland in the Northern North Sea.
Formely on the Forties Charlie platform, which is located ~110Miles NE of Aberdeen.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 PinmemberSBGTrading29-Jan-13 13:51 
GeneralRe: My vote of 5 PinmentorDaveAuld29-Jan-13 19:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 20 Jun 2010
Article Copyright 2010 by DaveAuld
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid