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

Autosize columns

2 Answers 345 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 03 Aug 2011, 03:31 PM
Hi,

I using a MVVM environment and try to achieve an auto size of all columns depending on a user preselected mode.
Therefore I've bound the RadGridView.ColumnWidth to a Property in the ViewModel. As I change the value of the property the values of the grid is also changing but the existing Columns are not resized. To force the resize if have iterate to each Column an change the Width.
But is not the best thing to do in an MVVM concept. Is there any better solution for that?

Regards
Thomas

2 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 05 Aug 2011, 11:25 AM
Hi Thomas,

 In order to change the column's Width you should bind the Width property of every column. For example if your ViewModel is like:

public class MyViewModel : INotifyPropertyChanged
    {
        private GridViewLength width = new GridViewLength(150);
        public GridViewLength Width
        {
            get { return this.width; }
            set
            {
                if (value != this.width)
                {
                    this.width = value;
                    this.OnPropertyChanged("Width");
                }
            }
        }

Then you may bind the Width like so:

<telerik:RadGridView Name="playersGrid" ItemsSource="{Binding Players}" AutoGenerateColumns="False">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{BindingName} Width="{Binding Width, Source={StaticResource MyViewModel}}"/>
                    </telerik:RadGridView.Columns>
                </telerik:RadGridView>

Additionally you have to use a StaticResource when you are binding to the Width property of the ViewModel.
 
Is this what are you looking for?

Best wishes,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Thomas
Top achievements
Rank 1
answered on 08 Aug 2011, 10:02 AM
Hi Didie,

this won't work for me as I'm using a data binding to a  DataView (Table.DefaultView). Which is a dynamic SQL filled table, so I use AutoGenerateColumns="true".

Regards
Thomas
Tags
GridView
Asked by
Thomas
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Thomas
Top achievements
Rank 1
Share this question
or