locked
Auto Resize Columns of ListView? RRS feed

  • Question

  • Is there a way to auto size your columns in a listview to show all their content after an item is added to the listview (as opposed to manually double clicking between each column)?  Thanks.
    Monday, June 4, 2007 4:11 PM

Answers

  • According to MSDN documentation for GridViewColumn, setting the column Width property to Double.NaN would autosize. However, if it's already autosized, it likely would ignore the reset. So, if you set the column's width to the current width (so it wouldn't actually resize),

     

    column.Width = column.ActualWidth;

     

    then reset the column to double.NaN,

     

    column.Width = double.NaN;

     

    it should auto size.

     

    You'll need to get the current view and loop through the columns when you've added your rows (something like this untested C# code):

      

    Code Snippet

    GridView gv = myListView.View as GridView;

    if (gv != null)

    {

      foreach(GridViewColumn gvc in gv.ColumnCollection)

      {

        gvc.Width = gvc.ActualWidth;

        gvc.Width = Double.NaN;

      }

    }

     

     

    Monday, June 4, 2007 6:16 PM

All replies

  • According to MSDN documentation for GridViewColumn, setting the column Width property to Double.NaN would autosize. However, if it's already autosized, it likely would ignore the reset. So, if you set the column's width to the current width (so it wouldn't actually resize),

     

    column.Width = column.ActualWidth;

     

    then reset the column to double.NaN,

     

    column.Width = double.NaN;

     

    it should auto size.

     

    You'll need to get the current view and loop through the columns when you've added your rows (something like this untested C# code):

      

    Code Snippet

    GridView gv = myListView.View as GridView;

    if (gv != null)

    {

      foreach(GridViewColumn gvc in gv.ColumnCollection)

      {

        gvc.Width = gvc.ActualWidth;

        gvc.Width = Double.NaN;

      }

    }

     

     

    Monday, June 4, 2007 6:16 PM
  • Good stuff.  I've been exasperated trying to set column headers to wrap and then having the entire column of data reduce its size to the new size of the wrapped header.

    In other words, the column starts out at width 200 because that's the size required to show the header on one line (the width is set to NaN so it should auto-size).  The data in the column only requires width 70 but is also set to 200 because of the header width requirement.  When I set the TextBlock for the header to wrap and set its width to 70 (more on that later), the entire column stays at with 200 - even the header.  the text within the header is width 70 and is wrapped, however.  I've found that setting the grid.visibility to hide and then show the column forces a redraw and the column re-renders at size 70.  I can't figure out what mechanism is being used to force that re-render and I can't exactly do the show/hide thing as that would look strange to users.

    Another issue is that I need to figure out what the minimum size of the column can be while not wrapping or clipping the column data - I only want to wrap the header data.  I can't figure out how to find the max minimum width of the column so none of the data cells are wrapped/clipped.

    Any ideas?

    Oh, by the way, my header values are actually multi-row headings...  A grid owning a stackpanel which owns many borders which each own a text block containing one row of the header text.

    CellDataColumnHeader
      Grid (PART_Grid)
         StackPanel (PART_HeaderColumns)
            Border
                TextBlock
            Border
                TextBlock
    Wednesday, August 26, 2009 8:04 PM
  • I would just like to say, WTF???

    How is it possible to assign a Double to Width, which is an Int?  This makes absolutely no sense.
    • Proposed as answer by Ghouri Wednesday, November 11, 2009 6:06 AM
    Friday, October 2, 2009 9:48 PM
  • private void SMS_Explorer_Resize(object sender, EventArgs e)
    {
      Resize_ListColumn(0);
    }
    void Resize_ListColumn(int list)
    {
           //Get the current column widths
          ArrayList widths = new ArrayList();
          if (list == 0)
            {
                 foreach (ColumnHeader ch in ilv.Columns)
                  {
                    widths.Add(ch.Width); 
                  } //end forwach
                //Get the total width of all the columns
                int total_width = 0;
                for (int i = 0; i < widths.Count; i++)
               {
                 total_width += (int)widths[i]; 
               } //end for
               //calculate percentages and resize the columns.
               for (int i = 0; i < widths.Count; i++)
              {
                double c_width = (int)widths[i];
                double pect = (c_width / total_width);
                //get the new width, leave out 25 pixels for scrollbar
                double n_width = (ilv.Width - 25) * pect;
                n_width = Math.Round(n_width, 0);
                //MessageBox.Show(c_width + " - " + pect + " - " + n_width);
                ilv.Columns[i].Width = (int)n_width;
               } //end for
           }//end if (list == 0)
          else
         {
                  // For Other List...
         }
    }
    //Here ilv is the ListView Control
    Wednesday, November 11, 2009 6:14 AM
  • Code Snippet

    GridView gv = myListView.View as GridView;

    if (gv != null)

    {

      foreach(GridViewColumn gvc in gv.ColumnCollection)

      {

        gvc.Width = gvc.ActualWidth;

        gvc.Width = Double.NaN;

      }

    }

    gv.ColumnCollection should be gv.Columns, then it'll work.

    Tuesday, June 24, 2014 3:01 PM