Programatically set the width of a DataColumn for use with a DataGrid
I assign a column programmatically to a DataTable like this:
myDataTable.Column开发者_开发知识库s.Add(myDataColumn);
Is there a way to programmatically set the width / size of the column?
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
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;
精彩评论