Home Forums WinForms controls Xceed Grid for WinForms Column Autosize Reply To: Column Autosize

User (Old forums)
Post count: 23064

If you’re trying to fit all of the columns inside the viewable area of the grid, here’s the method I wrote that makes them all equal sizes, and still fit in the viewable are of the grid:

Public Sub AdjustColumnWidths()
Dim column As Xceed.Grid.Column
Dim eachWidth As Integer
Dim visibleColumnCount As Integer = 0
For Each column In Columns
If column.Visible = True Then
visibleColumnCount = visibleColumnCount + 1
End If

If visibleColumnCount > 0 Then
eachWidth = (Width – 4) / visibleColumnCount
For Each column In Columns
column.Width = eachWidth
End If
End Sub

You could modify the logic here to step through the columns, set each to it’s fitted width, and add up the column widths as you go. Then, when you get to the last column, just check to see if your total calculated width is less than the width of the grid. If it is, you can subtract your total column width from the width of the grid, and that will tell you how big to make that last column, so you can fill the grid all the way across.

You could also modify this slightly to make all of the columns size to their fitted width, and then shrink each one of them by a fixed amount until they fit in the grid. That way your columns would at least be proportionally sized to one another, instead of all being a fixed size.


Imported from legacy forums. Posted by Christopher (had 655 views)