True DBGrid for WinForms | ComponentOne
Columns / Sizing
In This Topic
    Sizing
    In This Topic

    True DBGrid lets you set the size of grid columns, such as set the column width, auto-adjust the column width and set a minimum or maximum column width.

    Set Column Width

    True DBGrid provides the DefColWidth property of C1TrueDBGrid class to set the column width across the grid.

    Use the code below to set the default width of a column for True DBGrid.

    C#
    Copy Code
    // Setting default width of all columns
    c1TruedbGrid1.DefColWidth = 10;
    

    But if you have to set a particular column's width, use the code snippet below:

    C#
    Copy Code
    // Setting a column's width
    var col = c1TruedbGrid1.Splits[0].DisplayColumns[1];
    col.Width = 60;
    

    Auto-adjust Column Width

    To adjust the column width according to the text length, True DBGrid provides the AutoSize method in C1DisplayColumn class, which adjusts the width of a column to accommodate the longest visible field within that column.

    Following code shows how you can auto adjust the column widths according to the text length in the WinForms True DBGrid.

    C#
    Copy Code
    // Call Autosize for each column
    foreach (C1DisplayColumn col in c1TruedbGrid1.Splits[0].DisplayColumns)
    {
        col.AutoSize();
    }
    

    Set Minimum and Maximum Column Width

    True DBGrid allows you to set bounds to the column width by using the MinWidth property in C1DisplayColumn class, which gets or sets the minimum width a column can be resized to when in the C1.Win.TrueDBGrid.C1TrueDBGrid.SpringMode.

    Specify the bounds of column width in the WinForms True DBGrid using the code below.

    C#
    Copy Code
    // Set MinWidth for columns, to be used with SpringMode
    foreach (C1DisplayColumn column in c1TruedbGrid1.Splits[0].DisplayColumns)
    {
        column.MinWidth = 20; 
    }
    

    In this case, as mentioned above, the Spring mode should be enabled using the SpringMode property of C1TrueDBGrid class, which gets or sets a value that determines how columns will resize when the grid gets resized.

    The code snippet below depicts enabling or disabling SPringMode using a checkbox (named 'chckSpring').

    C#
    Copy Code
    private void chckSpring_CheckedChanged(object sender, EventArgs e)
    {
        //Enable/Disable spring mode
        c1TruedbGrid1.SpringMode = chckSpring.Checked;
    }