Home Forums WPF controls Xceed DataGrid for WPF Automatic sorting not working with Ent. Framework data

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #25827 |

    I’ve definitely seen the sorting work with this component, but when I have bound data from the Entity Framework classes, the sorting is unavailable. No error – just not there.

    I even tried to explicitly add the sorting feature in XAML but that didn’t pan out.

    You can see my sample code here.

    Thanks for any ideas.

    http://www.thedatafarm.com/blog/2008/01/28/BindingEFDataToTheFreeXceedWPFDataGrid.aspx

    Julie

    Imported from legacy forums. Posted by Julia (had 6398 views)

    Xceed Support
    Member
    Post count: 5658

    Hi Julie,

    The fact that the sorting is not working for you is due to a particular quirks of the design, which allow setting a “complex” statement as the FieldName of a Column.

    The first thing to say is that FieldName should remain, in most cases, the exact property name of the data item ( and not a complex statement ).

    To address a situation such as yours, you can use the DataGridCollectionView/DataGridCollectionViewSource, which provides additional services and optimizations for the DataGridControl ( in fact, we strongly suggest using the DGCVS, unless in particular exception cases ).
    <code>
    <Grid.Resources>
    <xcdg:DataGridCollectionViewSource x:Key=”dgcvs” Source=”{Binding}”>
    <xcdg:DataGridCollectionViewSource.ItemProperties>
    <xcdg:DataGridItemProperty Name=”CompanyName” ValuePath=”Customer.CompanyName” />
    <xcdg:DataGridItemProperty Name=”TotalDue” />
    </xcdg:DataGridCollectionViewSource.ItemProperties>
    </xcdg:DataGridCollectionViewSource>
    </Grid.Resource>
    </code>
    What this does is to specify to the DataGridCollectionView to expose to the DataGridControl the CompanyName and TotalDue “properties”. The CompanyName will be mapped automatically to the Customer.CompanyName sub property.

    The DataGridControl will then be able to perform Sorting and Grouping based on the CompanyName.

    All that is left to do is to bind the DataGridControl to the DGCVS instead of directly on the original data source:

    <code>
    <xcdg:DataGridControl ItemsSource=”{Binding Source={StaticResource dgcvs}}” />
    </code>

    While Nick’s comment on your blog about using the Column.DisplayMemberBinding also has its advantages ( sorting will be made on the “FieldName” property and not on the displayed value ), I think that using the DataGridCollectionView.ItemProperties will be the most versatile solution for your needs.

    Note: The code above was written from the top of my head and might contain some minor errors…. But in the essence, it should be right. 😉

    Imported from legacy forums. Posted by Marcus [Xceed] (had 391 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks Mark

    Let me make sure I understand.

    Identifying the field to bind to via the FIeldName is more of a convenience/shortcut to binding the way you are demonstrating in the above code. But when the fieldname is actually a property of a property (as in my case “Customer.CompanyName”) this breaks down? It works, the field gets bound, but some of the amenties such as sorting which depend on the field name wont’ work.

    Not a complaint – it seems to be an acceptable “feature”. I just want to make sure I am interpreting this correctly.

    Thanks!

    Imported from legacy forums. Posted by Julia (had 384 views)

    Xceed Support
    Member
    Post count: 5658

    Short Answer:

    You are right… FieldName is meant to “identify” the Column more than anything else.

    Longer, advanced answer (for people that likes to know the why ):

    It is only a “fallback” behavior that allow you to see content of the sub-property in the grid. Effectively, we use the FieldName property to build the default DisplayMemberBinding (if none are provided by the user ). Later on, when a SortDescription is built for the Column (when clicking on the column header), the SortDescription needs a Property name for the sorting to be effective but the composed syntax cannot resolve into an effective sorting.

    Using the DisplayMemberBinding approach ( such as Nick`s ) would allow you to sort by the “actual” value of the property (ID) but display some sub-properties of the object.

    Using the ItemProperties approach, you expose the sub-property as the real Column content, sorting will then be done on the sub-property`s value.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 6570 views)

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.