Home Forums WinForms controls Xceed Grid for WinForms How to change the width of columns

Viewing 1 post (of 1 total)
  • Author
  • User (Old forums)
    Post count: 23064
    #17811 |

    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.


    GridControl1.Columns( “ShipCountry” ).Width = 40


    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:


    Dim column As Column

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


    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:


    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


    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.

    Imported from legacy forums. Posted by Xceed admin (had 995 views)

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.