Xceed Grid for WinForms v4.3 Documentation
Welcome to Xceed Grid for WinForms v4.3 / Task-Based Help / Appearance / How to change the width of columns

In This Topic
    How to change the width of columns
    In This Topic

    The width of a column can be changed using either the Width property or the GetFittedWidth method. You can also retrieve the value necessary to display the entire contents of a cell using its GetFittedWidth method.

    Demonstration

    To change the width of a column the following code can be used: 

    VB.NET Copy Code
    GridControl1.Columns( "ShipCountry" ).Width = 40
    C# Copy Code
    gridControl1.Columns[ "ShipCountry" ].Width = 40;

    If you want to change the width of each column in the grid, you will need to do a "for each" to iterate through each one of the grid's columns. A common use would be to set the width of each column to its fitted width (the width necessary to display the entire content of the column) using the GetFittedWidth method. For example: 

    VB.NET Copy Code

    Dim column As Column 

    For Each column in GridControl1.Columns
      column.Width = column.GetFittedWidth()
    Next column

    C# Copy Code

    foreach( Column column in gridControl1.Columns )
    {
      column.Width = column.GetFittedWidth();
    }

    To make sure that the width of the columns stays to their fitted widths even if, for example, the length of one of the column's cell's value changes, you will need to call the GetFittedWidth method in each cell's ValueChanged event. For example:

    VB.NET Copy Code

    Dim cell As DataCell 

    For Each cell in GridControl1.DataRowTemplate.Cells
      AddHandler cell.ValueChanged, AddressOf Me.cell_ValueChanged
    Next cell 

    Private Sub cell_ValueChanged( ByVal sender As Object, ByVal e As EventArgs )
      Dim parentColumn As Column = CType( sender, DataCell ).ParentColum
      parentColumn.Width = parentColumn.GetFittedWidth()
    End Sub

    C# Copy Code

    foreach( DataCell cell in gridControl1.DataRowTemplate.Cells )
    {
      cell.ValueChanged += new EventHandler( this.cell_ValueChanged );

    private void cell_ValueChanged( object sender, EventArgs e )
    {
      Column parentColumn = ( ( DataCell )sender ).ParentColumn;
      parentColumn.Width = parentColumn.GetFittedWidth();      
    }

    If the DataRowTemplate changes while the DataSource property is set, the changes will not automatically be reflected in the grid. In order for the modifications to be applied, the data source must be reassigned to the DataSource property. 

    If you want to set various properties of the DataRowTemplate when you populate your grid (set the DataSource property), then this will need to be done between calls to the grid's BeginInit and EndInit methods. 

    If you are providing data manually, then you can set the values of the DataRowTemplate after you have added columns to the grid.