Home Forums WinForms controls Xceed Grid for WinForms Column Autosize

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #12804 |

    Hi,

    Is there a way to autosize columns in the grid. Also, is it possible to extend the last column to the right border of the grid (or to the scrollbar if present).

    Thanks

    Imported from legacy forums. Posted by cshaw (had 4427 views)

    User (Old forums)
    Member
    Post count: 23064

    Autosize columns: after filling the grid with data, just loop over the columns you want to autosize, and: column.Width = column.GetFittedWidth();

    Extend last column to right border: don’t know about that.

    Imported from legacy forums. Posted by Tommy (had 367 views)

    User (Old forums)
    Member
    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
    Next

    If visibleColumnCount > 0 Then
    eachWidth = (Width – 4) / visibleColumnCount
    For Each column In Columns
    column.Width = eachWidth
    Next
    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.

    ~Christopher

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

    User (Old forums)
    Member
    Post count: 23064

    Thanks for your replies, it was what I needed.

    Christophe

    Imported from legacy forums. Posted by cshaw (had 246 views)

    User (Old forums)
    Member
    Post count: 23064

    I see this is an old post, but here’s my method anyway:

    <code>
    private void AdjustGridColumnSizes()
    {
    int borderWidth = Convert.ToInt32( Math.Ceiling(this.gridControl1.GridLinePen.Width) );
    int remainingWidth = this.gridControl1.Width;
    int remainingHeight = this.gridControl1.Height;

    // subtract the row selector width
    remainingWidth -= (this.gridControl1.RowSelectorPane.Visible) ? (this.gridControl1.RowSelectorPane.Width + borderWidth) : 0;

    // subtract the vertical scrollbar width if it is showing
    remainingHeight -= (this.groupByRow1.Visible) ? (this.groupByRow1.Height + borderWidth) : 0;
    remainingHeight -= (this.cellcolumnManagerRow1column1.Visible) ? (this.columnManagerRow1.Height + borderWidth) : 0;
    foreach (Xceed.Grid.DataRow myRow in this.gridControl1.DataRows)
    {
    if (myRow.Visible)
    {
    remainingHeight -= myRow.Height + borderWidth;
    if (remainingHeight < 0)
    {
    remainingWidth -= SystemInformation.VerticalScrollBarWidth;
    break;
    }
    }
    }

    // set each column to its fitted width
    foreach (Xceed.Grid.Column myCol in this.gridControl1.Columns)
    {
    if (myCol.Visible)
    {
    myCol.Width = myCol.GetFittedWidth();
    remainingWidth -= myCol.Width + borderWidth;
    }
    }

    // set the last column to a width that will fill the rest of the panel
    if (remainingWidth > 0)
    {
    this.gridControl1.Columns[this.gridControl1.Columns.Count – 1].Width += remainingWidth;
    }
    }
    </code>

    I call this method when the grid is first loaded and also when the grid’s SizeChanged event is fired. Works like a charm.

    Imported from legacy forums. Posted by Lance (had 5548 views)

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.