Home Forums WPF controls Xceed DataGrid for WPF Set Columns to Auto Size to Smallest Width

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #27004 |

    I have am still working through the ins and outs of this great product.  One issue I am having  is that all the columns set themselves to the same width.  I have a grid on a window, with quite a few columns, and because all the columns are setting to equal widths (matching the largest), the resulting grid scrolls way off the page horizontally.  If I go through and double click all the column header splitters, they all resize down, and the grid ends up being about 1/2 the width.

    Is there any way to have the grids auto size themselves automatically?
     

    Imported from legacy forums. Posted by Scott (had 1001 views)

    Xceed Support
    Member
    Post count: 5658

    If you want the width of the columns to adjust to the content of their cells, you can set the Width property of the column to the result of its GetFittedWidth method. If you want to stretch columns, or have the last column fill the available space, that feature will be available in  the next version.

    Imported from legacy forums. Posted by Jenny [Xceed] (had 406 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks for the reply Jenny, a little more investigation led me to the GetFittedWidth() method.  I am now having an issue where GetFittedWidth always returns -1.

    Let me explain my set up  I am using a MVP pattern to display an estimate, and the datagrid is bound to a list of price details assigned to the currently selected estimate.

    So I have a view interface that my form implements…

    IEstimateView
    {
        Estimate CurrentEstimate {get; set;}
        ObservableCollection<PriceDetail> PriceDetails {get; set;}
    }

    My presenter, which my form contains one of…

    EstimatePresenter()
    {
        private IEstimateView _view;

        public EstimatePresenter(IEstimateView view)
        {
           _view = view;
        }

        public void LoadEstimate(Estimate estimate)
        {
           _view.CurrentEstimate = estimate;
           _view.PriceDetails = new ObservableCollection<PriceDetail>(estimate.PriceDetails.ToList<PriceDetail>());
        }
    }

    So, on my form I have a DataGridControlCollectionViewSource with it’s source set to the PriceDetails dependency property.  I have list box of estimates that calls _estimatePresenter.LoadEstimate(ListBox.SelectedItem as Estimate) when an item is selected, and this is where I am trying to set my column widths, but -1 is always the value…

    <xceed:DataGridCollectionViewSource x:Key=”PriceDetailCvs” Source=”{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}, Path=PriceDetailEntries}”/>

    <xceed:DataGridControl ItemsSource=”{Binding Source={StaticResource PriceDetailCvs}}”/>

    private void ListBox_SelectionChanged(object sender, RoutedEventArgs e)
    {   
        _estimatePresenter.LoadEstimate(ListBox.SelectedItem as Estimate);

        this.PriceDetailDataGrid.UpdateLayout();

        foreach (Column column in this.PriceDetailDataGrid.Columns)
           column.Width = column.GetFittedWidth();
    }

    Any suggestions?
     

    Imported from legacy forums. Posted by Scott (had 742 views)

    User (Old forums)
    Member
    Post count: 23064

    I should add, that the grid is in a tabcontrol, and the tabitem it’s in is not the primary tab, so it’s usually hidden until the user clicks that tab.  It seems to work if that tab has focus when the user selects a new estimate.  Is there a work around for this when the datagrid is on hidden tab item?

    I am assuming that GetFittedWidth() gets the width to fit the Title?  Because when it works, the column is sized to the title, and cuts off the actual cell values.

     

    Imported from legacy forums. Posted by Scott (had 506 views)

    Xceed Support
    Member
    Post count: 5658

    The tab will need to have the focus in order to calculate the fitted width of the columns. As for the GetFittedWidth method, it calculates according to the values of the cells that are currently visible in the viewport (the cells that are not in the view port do not exist and cannot be included in the calculation).

    Imported from legacy forums. Posted by Jenny [Xceed] (had 503 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks Jenny!

    Moved the UpdateLayout call, and set focus to the tab item, and it works well.
     

    Imported from legacy forums. Posted by Scott (had 1024 views)

    User (Old forums)
    Member
    Post count: 23064

    See the thread http://xceed.com/CS/forums/permalink/11646/13791/ShowThread.aspx#13791

    Imported from legacy forums. Posted by Diego (had 1725 views)

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.