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

Data binding columns to Arrays and DynamicObjects

4 Answers 800 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Edward Wilde
Top achievements
Rank 1
Edward Wilde asked on 30 Nov 2012, 01:58 PM
Hi,

I am creating a generic grid solution based on the Telerik grid as a prototype to test the suitability of the Telerik components.

Our data layer can not send us typed objects. Instead it sends an object with an array of field names and an array of values.

Arrays
My first approach was to use a binding expression referencing the index to display i.e:

DataMemberBinding="{Binding Path=Values[0]}"

The grid does retrieve the values using this expression, but grouping, sorting and filtering are missing.

Dynamic object
Since this didn't work I thought I could trick the grid into thinking the model was strongly typed using a dynamic object that returned values based on the binding name:

public override bool TryGetMember(GetMemberBinder binder, out object result)
{
    if (!this.AllFields.Contains(binder.Name))
    {
        return base.TryGetMember(binder, out result);
    }
 
    result = this.Values[this.AllFields.IndexOf(binder.Name)];
    return true;
}
This fixed grouping and sorting however filtering is still not working

I've have a simplified test project that demonstrates my findings, but the forum won't allow me to attach it. Any help resolving this issue would be most welcome.

4 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 30 Nov 2012, 02:20 PM
Hello,

If you go here and open RadGridView's example, there is an example called "Various Data Sources". The first available data source is a collection of DynamicObject's which shows how sorting, grouping and filtering all work properly. Please, compare your dynamic object implementation with the one in the example called MyDataRow which inherits from DynamicObject. What are the differences?

If nothing helps, you can open a new support ticket and attach your sample project there.

Greetings,
Rossen Hristov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Edward Wilde
Top achievements
Rank 1
answered on 30 Nov 2012, 03:12 PM
Hi,

Thanks I've checked my implementation of the Dynamic object and it's the same as the sample. The difference between my application is that data is not present during the bind operations.

This seems to not be a problem if you are binding to typed objects however if you have a collection of dynamic objects and the collection is empty at bind time the filters are not applied.

I will open a support case to take this further

Regards,

Ed.
0
Vlad
Telerik team
answered on 30 Nov 2012, 03:33 PM
Hi Ed,

 I've reviewed your project. You can avoid such problems if you set DataType for the grid columns:

xmlns:sys="clr-namespace:System;assembly=mscorlib"
...
<telerik:RadGridView x:Name="GridArray"
                            GroupRenderMode="Flat"
                            CanUserSortGroups="True"
                            IsSynchronizedWithCurrentItem="True"
                            IsReadOnly="True"
                            AutoGenerateColumns="False"
                            ItemsSource="{Binding Path=ArrayModels}">
        <telerik:RadGridView.Columns>
          <telerik:GridViewDataColumn Header="Name" IsFilterable="True" ShowFilterButton="True" IsSortable="True"
                                      DataMemberBinding="{Binding Path=Values[0]}" DataType="{x:Type sys:String}"/>
                <telerik:GridViewDataColumn Header="Hobby" DataMemberBinding="{Binding Path=Values[1]}" DataType="{x:Type sys:String}"/>
        </telerik:RadGridView.Columns>
      </telerik:RadGridView>
        <TextBlock Margin="0,20,20,5" FontFamily="Segoe UI Semibold" FontSize="15">Model with dynamic named properties</TextBlock>
      <telerik:RadGridView x:Name="GridArrayDynamic"
                       GroupRenderMode="Flat"
                       CanUserSortGroups="True"
                       IsSynchronizedWithCurrentItem="True"
                       IsReadOnly="True"
                       AutoGenerateColumns="False"
                        IsFilteringAllowed="True"
                       ItemsSource="{Binding Path=ArrayModelDynamics}">
        <telerik:RadGridView.Columns>
          <telerik:GridViewDataColumn Header="Name" 
                                      DataMemberBinding="{Binding Path=Name}" DataType="{x:Type sys:String}"/>
                <telerik:GridViewDataColumn Header="Hobby" DataMemberBinding="{Binding Path=Hobby}" DataType="{x:Type sys:String}" />
        </telerik:RadGridView.Columns>
      </telerik:RadGridView>


Kind regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Edward Wilde
Top achievements
Rank 1
answered on 30 Nov 2012, 03:47 PM
Thanks! The legendary Telerik support lives on....
Tags
GridView
Asked by
Edward Wilde
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Edward Wilde
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or