3

I want to set programmatically my GridViewColumns to autosize by content, not by header (Width = double.NaN)

I have searched for a long time and I found this problem solved with DataGridColumns, but how is this with GridViewColumns ?

H.B.
  • 166,899
  • 29
  • 327
  • 400
Gepro
  • 583
  • 1
  • 11
  • 20

2 Answers2

2

I had the same problem, found a good hint on it here.

This is how I solved the problem.

if ((sender as ListView)?.View is GridView gridview)
{
    foreach (var column in gridview.Columns)
    {
        // Set the Width. Then clear it to cause the autosize.
        column.Width = 1;
        column.ClearValue(GridViewColumn.WidthProperty);
    }
}
BenVlodgi
  • 2,135
  • 1
  • 13
  • 26
1

Try this:

foreach (DataGridColumn column in grid.Columns) {
      column.Width = DataGridLength.SizeToCells;
}
ionden
  • 12,536
  • 1
  • 45
  • 37
  • 3
    I have GridViewColumns from a GridView in a ListView, not DataGridColumns from a DataGrid – Gepro Mar 22 '12 at 12:38