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

Seperate Collection binding per GridViewDataColumn possible?

3 Answers 32 Views
GridView
This is a migrated thread and some comments may be shown as answers.
ara
Top achievements
Rank 2
ara asked on 27 Nov 2013, 03:09 PM
Hello,

I’ve started my RadControl a few days ago and I’am delighted by the number feature and mature design of the components.
However I've also just started with WPF UI Development, so my learning curve is still steep and maybe you can help me with a Data Binding problem/misunderstanding of mine:
I've got a RadGridView (inside a Data Template) in an MVVM Application, and I’m trying to bind an Observable Collection to a GridViewData Column. Please take a look at my following Models and View-Models (I’ve stripped out unnecessary code):

//Part of the ViewModel - Begin
private ObservableCollection<ProductCategoryModel> _planCollection = null;
public ViewModel()
{
    //ViewModel Constructor
    _planCollection = new ObservableCollection<ProductCategoryModel>
                {
                    new ProductCategoryModel("AlphaPlan"),
                    new ProductCategoryModel("BetaPlan")
                }
}
public ObservableCollection<ProductCategoryModel> PlanCollection
{
    get { return _planCollection; }
    set { _planCollection = value; }
}
public ObservableCollection<CalendarWeek> AlphaPlan
{
    get { return _planCollection[0].Numbers; }
    set { throw new NotImplementedException(); }
}
public ObservableCollection<CalendarWeek> BetaPlan
{
    get { return _planCollection[1].Numbers; }
    set { throw new NotImplementedException(); }
}
//Part of the ViewModel - End
 
//The Models - Begin
public class CalendarWeek : INotifyPropertyChanged
    {
        public CalendarWeek(int week, float value)
        {
            this.Week = week;
            this.Value = value;
        }
 
        [Display(AutoGenerateField = false)]
        public int Week { get; set; }
 
        [DisplayAttribute(Name = "Value")]
        public double Value { get; set; }
    }
 
public class ProductCategoryModel
    {
        public ProductCategoryModel(string leName)
        {
            this.Product = leName;
            this.Numbers = new ObservableCollection<CalendarWeek>();
 
            for (int i = 1; i <= 52; i++)
            {
                Random r = new Random(Guid.NewGuid().GetHashCode());
                int rInt = r.Next(0, 100);
                this.Numbers.Add(new CalendarWeek(i, rInt));
            }
        }
 
        public ObservableCollection<CalendarWeek> Numbers { get; set; }
        public string Product { get; set; }
    }
//The Models - End


I want to put these Collections in one RadGridView (as you can see, I just want the Values member from the CalendarWeek in the Column).
I have tried many approaches I have stumbled across the RadControl Help, Forums or on StackOverflow, but none seems too work (or I missed the right combination) for me.

What are your preferred approach here?

Hierarchical? Master Detail Binding? Creating my own GridViewDataColumn Template/Style?

Thanks a lot and happy Holidays for all of you :)
Julien

3 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 28 Nov 2013, 12:22 PM
Hello Julien,

You could specify the binding of a column to be a collection. You can also define your own DataTemplate as a CellTemplate to visualize the collection as you would like to. Please refer to our online documentation. If you want to display a different template for the different cells, then you could apply a CellTemplateSelector for the column.

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
ara
Top achievements
Rank 2
answered on 01 Dec 2013, 05:13 PM
Hello Didie,

thank you for your answer, sorry but i cant figure out how to bind a specific collection to a specific DataColumn.
<Grid x:Name="GridMaze" Grid.Row="1">
    <telerik:RadGridView Grid.Column="0" x:Name="LeftPlanGrid" AutoGenerateColumns="False">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding FirstDataColumn}"
                                        Header="{Binding FirstComboSelect}"/>
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>
</Grid>

FirstDataColumn returns a ObservableCollection of doubles, FirstComboSelect returns the Column name as string (which just works fine).
Does DataMemberBinding should work without an ItemsSource in GridView? If no, would it make sense to put the separate Collections into an CompositeCollection, which would work as ItemsSource for the GridView?

From a logical PoV i should create several GridViews, instead of adding columns to one.
But since the Grid should "look and feel" like one table, i found this approach more convenient.

Thanks again for your help.

Sincerly,
Julien
0
Dimitrina
Telerik team
answered on 02 Dec 2013, 02:13 PM
Hi Julien,

The GridViewDataColumn will not generate a RadGridView to display the data if it is bound to a collection. It will display the ToString() value of the bound object. If you require this, then you should place a proper control as a column's CellTemplate. 

My suggestion would be to define different RadGridViews and place them in a Grid with proper RowDefinitions/ColumnDefinitions so that they are aligned the way you require.

Regards,
Didie
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
ara
Top achievements
Rank 2
Answers by
Dimitrina
Telerik team
ara
Top achievements
Rank 2
Share this question
or