开发者

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜