patrick
patrick

Reputation: 16959

Programatically set the width of a DataColumn for use with a DataGrid

I assign a column programmatically to a DataTable like this:

myDataTable.Columns.Add(myDataColumn);

Is there a way to programmatically set the width / size of the column?

Upvotes: 3

Views: 15755

Answers (2)

fab
fab

Reputation: 2509

This resizes N-1 columns to "Auto" and column N to "Fill"

foreach (var column in dataGrid.Columns)
    column.Width = DataGridLength.Auto;
dataGrid.Columns.Last().Width = DataGridLength.SizeToCells;

Upvotes: 1

Scott
Scott

Reputation: 2193

ColumnDefinition col1 = new ColumnDefinition();
col1.Width = GridLength.Auto;
ColumnDefinition col2 = new ColumnDefinition();
col2.Width = new GridLength(1,GridUnitType.Star);

grid.ColumnDefinitions.Add(col1);
grid.ColumnDefinitions.Add(col2);

top pieces will auto size columns, bottom piece you can customize size. look into this site for more detail -- http://www.wpftutorial.net/GridLayout.html

Upvotes: 8

Related Questions