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

Xceed Support
Member
Post count: 5658

There is no direct way to do that with our GridControl.

Here is a basic implementation of what you could do.

<code>
private void Form1_Load( object sender, EventArgs e )
{
Column col1 = new Column( “Column1” );
Column col2 = new Column( “Column2” );
gridControl1.Columns.Add( col1 );
gridControl1.Columns.Add( col2 );

for( int i = 0; i < 20; i++ )
{
Xceed.Grid.DataRow row = gridControl1.DataRows.AddNew();
foreach( Cell cell in row.Cells )
{
cell.Value = “Item ” + i.ToString();
}
row.EndEdit();
}

foreach( Column col in gridControl1.Columns )
{
col.WidthChanged += new EventHandler( col_WidthChanged );
}

//gridControl1.BorderStyle = BorderStyle.None;
gridControl1.Columns[ gridControl1.Columns.Count – 1 ].Width = gridControl1.DisplayRectangle.Width – gridControl1.Columns[ 0 ].Width
– gridControl1.RowSelectorPane.Width – gridControl1.FixedColumnSplitter.Width;
gridControl1.Resize += new EventHandler( gridControl1_Resize );
}

void col_WidthChanged( object sender, EventArgs e )
{
if( ( ( Column )sender ).Index != gridControl1.Columns.Count – 1 )
{
gridControl1.Columns[ gridControl1.Columns.Count – 1 ].Width = gridControl1.DisplayRectangle.Width – gridControl1.Columns[ 0 ].Width
– gridControl1.RowSelectorPane.Width – gridControl1.FixedColumnSplitter.Width;
}
}

void gridControl1_Resize( object sender, EventArgs e )
{
gridControl1.Columns[ gridControl1.Columns.Count – 1 ].Width = gridControl1.DisplayRectangle.Width – gridControl1.Columns[ 0 ].Width
– gridControl1.RowSelectorPane.Width – gridControl1.FixedColumnSplitter.Width;
}
</code>

Imported from legacy forums. Posted by CharlesB (had 4448 views)