This is a migrated thread and some comments may be shown as answers.

Adjusting column widths when setting a new ItemsSource

8 Answers 210 Views
GridView
This is a migrated thread and some comments may be shown as answers.
GEB
Top achievements
Rank 1
GEB asked on 04 Oct 2010, 01:42 PM
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.

8 Answers, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 05 Oct 2010, 03:04 PM
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
0
GEB
Top achievements
Rank 1
answered on 05 Oct 2010, 08:20 PM
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. 
0
Vlad
Telerik team
answered on 06 Oct 2010, 07:12 AM
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
0
Syed Danish
Top achievements
Rank 1
answered on 06 Oct 2010, 08:18 AM
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
0
GEB
Top achievements
Rank 1
answered on 06 Oct 2010, 03:44 PM
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).
0
Vlad
Telerik team
answered on 12 Oct 2010, 01:56 PM
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
0
Pardeep
Top achievements
Rank 1
answered on 10 Jul 2015, 01:00 PM

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.

0
Dimitrina
Telerik team
answered on 13 Jul 2015, 01:26 PM
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
Tags
GridView
Asked by
GEB
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
GEB
Top achievements
Rank 1
Vlad
Telerik team
Syed Danish
Top achievements
Rank 1
Pardeep
Top achievements
Rank 1
Dimitrina
Telerik team
Share this question
or