Home Forums WPF controls Xceed DataGrid for WPF Column auto-sizing according to the host control size

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

    I’m trying to have in my data grid control columns which will change their width respectively to the actual  width of the data grid control (which host them). At the moment I’m creating the columns in code-behind, and setting up a binding on the Column WidthProperty using converter (with number of columns as ConverterParameter). Performances are not so good, that is why I’m looking for something else in order to solve this request. 

    Is there any solution for getting auto sizing of column (width of the column) in a data grid control respectively to the width of the column host control (i.e. – the “actual width” of the DataGrid control)? 

    p.s. – I”m using XceedDataGridControl v.2

    Imported from legacy forums. Posted by einav (had 4478 views)

    Xceed Support
    Member
    Post count: 5658

    Column Auto-Sizing is not supported in version 2.0 of the DataGrid for WPF.

    Support for Column Auto-Sizing is currently planned for version 2.1.

    For more details on version 2.1, please refer to the roadmap, which can be found here:
    http://xceed.com/CS/forums/thread/11207.aspx

    Imported from legacy forums. Posted by Marcus [Xceed] (had 4058 views)

    User (Old forums)
    Member
    Post count: 23064

    Hello

    I created a control that is very similar to http://xceed.com/CS/forums/thread/8224.aspx.
    The 1st version of the control worked for pages with no hidden grids, but when these became visible not worked. (bad for me :()

    this custom version works with all grids of the page, and when they become visible automatically adjust the columns (the trick is in the loaded because GetFittedWidth always return a valid value)

     

    Any sugestions email-me

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Xceed.Wpf.DataGrid;
    using System.Windows.Controls;
    using System.Collections;


    namespace etcetcetc.Grid
    {
    public class ctrGrid : Xceed.Wpf.DataGrid.DataGridControl
    {
    private bool RunAutoFit = false;
    public ctrGrid()
    : base()
    {
    this.Loaded += new System.Windows.RoutedEventHandler(ctrGrid_Loaded);

    this.LayoutUpdated += new EventHandler(Grid_LayoutUpdated);
    }

    void ctrGrid_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
    this.RunAutoFit = true;
    Grid_LayoutUpdated(null, null);
    }


    private void Grid_LayoutUpdated(object sender, EventArgs e)
    {
    if (this.RunAutoFit && this.IsVisible)
    {
    AutoFit();
    this.RunAutoFit = false;
    }
    }

    public void AutoFit()
    {
    foreach (Column col in this.Columns)
    {
    double fittedWidth = col.GetFittedWidth();

    if (fittedWidth > 0)
    col.Width = fittedWidth + 2;
    }
    }

    protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue)
    {
    UpdateLayout();
    base.OnItemsSourceChanged(oldValue, newValue);

    this.RunAutoFit = true;
    }
    }
    }

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

    Xceed Support
    Member
    Post count: 5658

    As a precision this feature (column stretching and starrable column widths) was introduced in version 3.0.

    Imported from legacy forums. Posted by Vince [Xceed] (had 796 views)

    User (Old forums)
    Member
    Post count: 23064

    That’s nice. But how does one do it? 

     In my case I have a particular grid that has dynamic columns, so I can’t specify the columns or widths.

     What I would really like is the column to shrink to whatever is biggest, the column header name or the content.

    Imported from legacy forums. Posted by Ross (had 3367 views)

    User (Old forums)
    Member
    Post count: 23064

    [quote user=”Ross”]

    That’s nice. But how does one do it? 

    [/quote]

    +1 … Why is it so hard to find information on auto-sizing columns?

    Imported from legacy forums. Posted by Jared (had 2457 views)

    User (Old forums)
    Member
    Post count: 23064

    I am also loading Tables into the Grid dynamically.  Was there a way to have the column autosize depending on the data that is in the Grid?  (I would also like the Columns to autosize to the largest cell even if its not in the viewport.

    Imported from legacy forums. Posted by David (had 3194 views)

    User (Old forums)
    Member
    Post count: 23064

    I made a small modification to this class that might help some users experiencing similar problems to me:

    http://www3.xceed.com/CS/forums/permalink/25546/25546/ShowThread.aspx#25546

    Imported from legacy forums. Posted by Neo (had 819 views)

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