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

Unable to Sort Basic GridView

6 Answers 213 Views
GridView
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 24 Feb 2011, 10:52 PM
We just got the WPF package last week, so it should be the latest release.

We've set up a basic GridView, but it doesn't allow us to sort the columns.

The XAML looks like this:
<telerik:RadGridView HorizontalAlignment="Stretch" Margin="10,10,10,0" x:Name="ProjectGrid" VerticalAlignment="Stretch" AutoGenerateColumns="False" AlternateRowBackground="#FFEEEEEE" RowIndicatorVisibility="Collapsed" CanUserSortColumns="True" IsReadOnly="True" CanUserFreezeColumns="False" SelectionUnit="FullRow" CanUserResizeColumns="False" IsFilteringAllowed="True" ItemsSource="{Binding  Path=CurrentUser.Quotes, Source={StaticResource applicationViewModel}}">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="Project Date" Width=".15*" DataMemberBinding="{Binding Path=ProjectDate}"  DataFormatString="{}{0:MM/dd/yyyy}" />
                <telerik:GridViewDataColumn Header="Customer" Width=".2*" DataMemberBinding="{Binding Path=CustomerName}" />
                <telerik:GridViewDataColumn Header="PO #" Width=".1*" DataMemberBinding="{Binding Path=POnumber}" />
                <telerik:GridViewDataColumn Header="Quote #" Width=".1*" DataMemberBinding="{Binding Path=QuoteNumber}" />
                <telerik:GridViewDataColumn Header="Quote Type" Width=".15*" DataMemberBinding="{Binding Path=QuoteType.Description}" />
                <telerik:GridViewDataColumn Header="User" Width=".2*">
                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Path=User.FirstName}" Width="Auto"></TextBlock>
                                <TextBlock Text="{Binding Path=User.LastName}" Margin="5,0,0,0" Width="Auto"></TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>
                </telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Items" Width=".1*" DataMemberBinding="{Binding QuoteProducts.Count}" />
            </telerik:RadGridView.Columns>
            <mcmds:CommandBehaviorCollection.Behaviors>
                <mcmds:BehaviorBinding Event="MouseDoubleClick" Command="{Binding DoubleClick}" CommandParameter="{Binding SelectedItem, ElementName=ProjectGrid}" />
            </mcmds:CommandBehaviorCollection.Behaviors>
        </telerik:RadGridView>

I've tried setting a SortMemberPath on the columns, but that doesn't buy me any ground. I looked over the GridView documentation on sorting, but I didn't notice anything out of place. 

My colleague set up the attached behavior at the bottom. I may have misheard him, but I believe he said he pulled it from these forums, posted from your team. Basically, we needed to know when the user double-clicked a row, and that does it for us. I tried removing it as well (just in case it was causing the problem), but it didn't change anything.

Thanks!

6 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 25 Feb 2011, 08:20 AM
Hello David,

Generally, there should be no problem to perform soring. However, please keep in mind that your collection should implement either IComparable Interface or IEquatable Interface in order to be able to use this functionality.
 

Best wishes,
Maya
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
David
Top achievements
Rank 1
answered on 25 Feb 2011, 08:21 PM
Maya,

Thanks for taking the time to reply. Although I must say that I'm still kind of at a loss. The data we're binding to (CurrentUser.Quotes) is an EntityFramework 4 entity object, and Quotes is a navigation property on that object. 

So, if I am understanding your reply correctly, the Quote type must implement the IComparable interface. So, if my understanding of the interface is correct, by implementing this interface, we should be able to sort objects of type Quote, because they will know how to determine if one Quote is greater than or less than another quote. But will this give us the ability to sort the quotes based on more than one property (although certainly only one at a time)? It seems like IComparable will only allow one method by which to compare the objects themselves.

I may have mis-stated the intention in the original post, but the end goal is to be able to click the column heading and have it order the rows based on the specific property of the quote displayed in that column. My apologies if I am overlooking something silly, but thank you for your time.
0
Maya
Telerik team
answered on 02 Mar 2011, 12:35 PM
Hello David,

You may implement IComparable with multiple sorting options as demonstrated here for example. However, it seems to me that you should be able to perform sorting by default in your case. Will it be possible to send a sample project (via support ticket) reproducing this behavior so that I can debug it locally and provide a suitable solution ? 

 

Kind regards,
Maya
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Julio
Top achievements
Rank 1
answered on 07 Jun 2011, 05:47 PM

Hello Maya, David

We were using the trial version q2 2010 sp2 and the implementing to EF 4.0 was succesfull, then we decided bought ultimate premium version ( q1, 2011 sp2 ) and we have the same problem that describe David when migrate to the new version.

We apply this hot fix  wpf_40_2011_1_0516_Dev_hotfix and not resolve the problem.

Maya, what more I can do?
0
Maya
Telerik team
answered on 08 Jun 2011, 01:08 PM
Hello Julio,

I have tried to reproduce the issue, but without any success. May you take a look at the sample attached to verify whether it is corresponding to your exact requirements ? Are there any additional steps that I need to follow in order to get the same behavior as the one described by you ?
 

Kind regards,
Maya
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
Bogdan Dumitrescu
Top achievements
Rank 1
answered on 26 Jul 2011, 03:42 PM
Before reading my post please note that I cannot reproduce the issue in another project other the one I'm working on, yet I will describe the behaviou hoping that you can figure out what's happening.

So, filtering and sorting do not work in a very simple grid. When the grid is in this state, doubleclicking a cell tries to switch to edit mode and throws ("A TwoWay or OneWayToSource binding cannot work on the read-only property.") despite the binding being set to a readonly property using DataMemberBinding. That indicates that a TwoWay binding is generated, basically ignoring the public interface of my view model.

To isolate the issue, I commented out all the grid columns apart from one which is a string readonly property returning a constant value.

The control containing the grid is bound to a GridViewModel class which has an Items property of type IEnumerable<IGridItemViewModel>.
I have 2 types of view models, TypeAViewModel and TypeBViewModel both of them inheriting from an abstract base class BaseItemViewModel. BaseItemViewModel contains abstract implementations of the properties defined in the IGridItemViewModel interface and those properties are then overridden in the TypeAViewModel and TypeBViewModel classes. I also have 2 more interfaces ITypeAViewModel and ITypeBViewModel both of them implementing IGridItemViewModel and being respectively implemented by TypeAViewModel and TypeBViewModel.

I had no issue with this grid before upgrading to the new version last week. There are a dozen other grids in my project which all work fine. The only thing I can see particular to this grid is that it displays 2 types of view models.
Interestingly, if I only load view models of one type from the db (without changing the inheritance structrure) it all works. As soon as I have view models of different types sorting and filtering stop working.

Commenting out all the code in the BaseItemViewModel class doesn't help. However not inheriting from BaseItemViewModel fixes the issue.

The following code illustrates my class hierarchy in the real project I'm working on but once again I couldn't replicate the issue in a test proejct i created which uses the very same class hierarchy. Hope that helps.

public class TypeAViewModel : BaseGridItemViewModel<TypeAData>, ITypeAViewModel
{
....
public override string Name
{
  get { return "Type A"; }
}
....
}


public class TypeBViewModel : BaseGridItemViewModel<TypeBData>, ITypeBViewModel
{
....
public override string Name
{
  get { return "Type B"; }
}
....
}

public class BaseGridItemViewModel<TData> : IGridItemViewModel
{
  public abstract string Name { get; }
}

public interface ITypeAGridViewModel : IGridItemViewModel
{
.....
}
public interface ITypeBGridViewModel : IGridItemViewModel
{
.....
}
 
public interface IGridItemViewModel
{
 string Name { get; }
}

public class GridViewModel
{
 
private List<IGridItemViewModel> _items;
    
public IEnumerable<IGridItemViewModel> Items
  {
    get  {
          if (_items == null)
               {
                 LoadData();
              }
            return _items;
         }
  }
 
...........
 
}

<UserControl>
.....
<telerik:RadGridView ItemsSource="{Binding Items}" AutoGenerateColumns="False">
   <telerik:RadGridView.Columns>
      <telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name}" />
   </telerik:RadGridView.Columns>
</telerik:RadGridView>
.....
</UserControl>
Tags
GridView
Asked by
David
Top achievements
Rank 1
Answers by
Maya
Telerik team
David
Top achievements
Rank 1
Julio
Top achievements
Rank 1
Bogdan Dumitrescu
Top achievements
Rank 1
Share this question
or