Adjusting column widths when setting a new ItemsSource

9 posts, 0 answers
  1. GEB
    GEB avatar
    228 posts
    Member since:
    Dec 2008

    Posted 04 Oct 2010 Link to this post

    I have a RadGridView where I'm setting the ItemsSource in code-behind, and is an EntityCollection.  All columns in the grid are defined in XAML, and all column widths are "Auto" with the exception of the last column, where the width is set to "*".  When I initially set ItemsSource of the grid, the column widths are set correctly. 

    Now, depending on the user's actions, the ItemsSource of the grid may be set to a new EntityCollection.  What I have noticed is that the column widths remain as they were with the previous ItemsSource.  That is, the column widths don't seem to adjust themselves automatically when a new ItemsSource is set for the Grid.  Is there any way in code-behind to force the Grid to use the new ItemsSource when setting the column widths?  I would think that this would be something that the RadGridView would do automatically when it's ItemsSource is reset.
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 05 Oct 2010 Link to this post

    Hello GEB,

    Indeed RadGridView would  preserve the columns state, when changing the itemssource with one with the same item type.

    The folowing small hack will force the columns to autofit to the new content ( we toggle the width mode ,in order to force the column to rearrange)
    foreach (var column in this.radGridView.Columns)
                {
                    column.Width = GridViewLength.SizeToHeader;
                    column.Width = GridViewLength.Auto; ;
                  }

    Regards,
    Pavel Pavlov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. GEB
    GEB avatar
    228 posts
    Member since:
    Dec 2008

    Posted 05 Oct 2010 Link to this post

    Thank you for the quick response.  This works great, except that I want to set the column width of the last column in the collection to "*".  I know how to determine if it is the last column, but I'm not sure how to specify a width equal to "*".  There doesn't seem to be a GridViewLength enum for this purpose. 
  5. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 06 Oct 2010 Link to this post

    Hello,

     Here is an example:

    Width = new GridViewLength(1, GridViewLengthUnitType.Star)

    Sincerely yours,
    Vlad
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Syed Danish
    Syed Danish avatar
    70 posts
    Member since:
    Dec 2009

    Posted 06 Oct 2010 Link to this post

    Hello GEB,
    For the Columns of the Grid to be of the width depending on the text it contains set the column width to Width = new GridViewLength(1, GridViewLengthUnitType.Auto) and as u said that the the last column of the grid needs to be stretched till the end do give the width to that column as Width = new GridViewLength(1, GridViewLengthUnitType.Star). Here the 1 as similar with that of "1*", where the column widths will be automatically be divided equally and the available space will be multiplied with the number specified, as the parameter.

    If any problem get back to me.

    Thanks and Regards,
    Syed Danish
  7. GEB
    GEB avatar
    228 posts
    Member since:
    Dec 2008

    Posted 06 Oct 2010 Link to this post

    Vlad, I made this change, but the issue I'm running into is that the grid no longer scrolls horizontally once this change was made.  In my case, the contents of the grid are wider that the grid display, which means that a horizontal scrollbar should be visible.  Instead, the last three columns of the grid are shortened, and no horizontal scrollbar is visible.  This does not occur if I remove the code that attempts to reset column widths as the ItemsSource is reset.

    I use GridViewLength.Auto for all columns except for the last column.  On the last column, I use GridViewLength(1, GridViewLengthUnitType.Star).
  8. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 12 Oct 2010 Link to this post

    Hi,

     Generally you can have horizontal scrollbar with star column(s) only if you have lots of columns or enough wide columns. 

    Best wishes,
    Vlad
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  9. Pardeep
    Pardeep avatar
    1 posts
    Member since:
    Nov 2012

    Posted 10 Jul 2015 in reply to Pavel Pavlov Link to this post

    Hi Pavel,

    I have also same problem as GEB. Please let me know where do we have to apply this code force the columns to autofit to the new content.

  10. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 13 Jul 2015 Link to this post

    Hi,

    You can try this on DataLoaded event raised after resetting the ItemsSource.

    Regards,
    Dimitrina
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
DevCraft banner