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

UserSortMode = Single does not work with DataGridTemplateColumn

2 Answers 72 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
Safan
Top achievements
Rank 1
Safan asked on 24 Jun 2019, 03:26 PM

Hi,

I have a data grid with UserSortMode set to "Single".

If i use my columns as DataGridTextColumn every thing works as expected.

If I modify my column as DataGridTempateColumn and Specify SortDescriptor as "DelegateSortDescriptor", Data is sorted as expected, but the sort icon in column header remains as is.

Please find the code snippet below.

Steps to regenerate

  • Open the application
  • Sort "Id" Column (Ascending)
  • Immediately sort "Name" column 

Note that Name column is sorted, but notice that the sort icon on "Id" column is retained, which is not expected.

Please have a look at the issue and point me if i am missing any thing.

 

Code Snippet

>>>>>>>>>>>>

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:telerik="clr-namespace:Telerik.XamarinForms.DataGrid;assembly=Telerik.XamarinForms.DataGrid"
             xmlns:local="clr-namespace:GridSorting"
             x:Class="GridSorting.Views.MainPage"
             Title="{Binding Title}">

    <StackLayout>
        <telerik:RadDataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False" SelectionMode="Single" UserSortMode="Single">
            <telerik:RadDataGrid.SelectionStyle>
                <telerik:DataGridBorderStyle BackgroundColor="LightGoldenrodYellow" BorderColor="DarkBlue"/>
            </telerik:RadDataGrid.SelectionStyle>
            <telerik:RadDataGrid.Columns>

                <!--<telerik:DataGridNumericalColumn PropertyName="Id" HeaderText="ID"/>-->
                <telerik:DataGridTemplateColumn HeaderText="ID">
                    <telerik:DataGridTemplateColumn.SortDescriptor>
                        <local:MySortDescriptor PropertyName="Id" />
                    </telerik:DataGridTemplateColumn.SortDescriptor>
                    <telerik:DataGridTemplateColumn.CellContentTemplate>
                        <DataTemplate>
                            <Grid Padding="10">
                                <Label Text="{Binding Id}"/>
                            </Grid>
                        </DataTemplate>
                    </telerik:DataGridTemplateColumn.CellContentTemplate>
                </telerik:DataGridTemplateColumn>

                <!--<telerik:DataGridTextColumn PropertyName="Name" HeaderText="Name"/>-->
                <telerik:DataGridTemplateColumn HeaderText="Name">
                    <telerik:DataGridTemplateColumn.SortDescriptor>
                        <local:MySortDescriptor PropertyName="Name" />
                    </telerik:DataGridTemplateColumn.SortDescriptor>
                    <telerik:DataGridTemplateColumn.CellContentTemplate>
                        <DataTemplate>
                            <Grid Padding="10">
                                <Label Text="{Binding Name}"/>
                            </Grid>
                        </DataTemplate>
                    </telerik:DataGridTemplateColumn.CellContentTemplate>
                </telerik:DataGridTemplateColumn>

                <!--<telerik:DataGridTextColumn PropertyName="City" HeaderText="City"/>-->
                <telerik:DataGridTemplateColumn HeaderText="City">
                    <telerik:DataGridTemplateColumn.SortDescriptor>
                        <local:MySortDescriptor PropertyName="City" />
                    </telerik:DataGridTemplateColumn.SortDescriptor>
                    <telerik:DataGridTemplateColumn.CellContentTemplate>
                        <DataTemplate>
                            <Grid Padding="10">
                                <Label Text="{Binding City}"/>
                            </Grid>
                        </DataTemplate>
                    </telerik:DataGridTemplateColumn.CellContentTemplate>
                </telerik:DataGridTemplateColumn>
            </telerik:RadDataGrid.Columns>
        </telerik:RadDataGrid>
    </StackLayout>

</ContentPage>

 

namespace GridSorting
{
    public class MySortDescriptor : DelegateSortDescriptor
    {
        public MySortDescriptor()
        {
            KeyLookup = new MyKeyLookUp(this);
            Comparer = new MyComparer();
        }

        public string PropertyName { get; set; }
    }

    public class MyKeyLookUp : IKeyLookup
    {
        private MySortDescriptor mySortDescriptor;

        public MyKeyLookUp(MySortDescriptor mySortDescriptor)
        {
            this.mySortDescriptor = mySortDescriptor;
        }

        public object GetKey(object instance)
        {
            var objectAccessor = ObjectAccessor.Create(instance);
            return objectAccessor[mySortDescriptor.PropertyName];
        }
    }

    public class MyComparer : IComparer
    {
        public int Compare(object x, object y)
        {
            return string.Compare(x.ToString(), y.ToString());
        }
    }
}

 

namespace GridSorting.ViewModels
{
    public class MainPageViewModel : ViewModelBase
    {
        private ObservableCollection<MainPageModel> _items;

        public MainPageViewModel(INavigationService navigationService)
            : base(navigationService)
        {
            Title = "Main Page";

            Items = new ObservableCollection<MainPageModel>(new List<MainPageModel>()
            {
                new MainPageModel() { Id = 1, Name = "Name 1", City = "City 1" },
                new MainPageModel() { Id = 2, Name = "Name 2", City = "City 2" },
                new MainPageModel() { Id = 3, Name = "Name 3", City = "City 3" },
                new MainPageModel() { Id = 4, Name = "Name 4", City = "City 4" },
                new MainPageModel() { Id = 5, Name = "Name 5", City = "City 5" },
                new MainPageModel() { Id = 6, Name = "Name 6", City = "City 6" },
                new MainPageModel() { Id = 7, Name = "Name 7", City = "City 7" },
                new MainPageModel() { Id = 8, Name = "Name 8", City = "City 8" },
                new MainPageModel() { Id = 9, Name = "Name 9", City = "City 9" },
                new MainPageModel() { Id = 10, Name = "Name 10", City = "City 10" },
            });
        }

        public ObservableCollection<MainPageModel> Items
        {
            get { return _items; }
            set { SetProperty(ref _items, value); }
        }
    }

    public class MainPageModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
    }
}

<<<<<<<<<<<<

Thank you.

2 Answers, 1 is accepted

Sort by
0
Accepted
Nasko
Telerik team
answered on 27 Jun 2019, 11:39 AM
Hello Mohamed,

Thank you for the provided detailed information. Using it I was able to reproduce the described issue.

However, I am glad to inform you that during our research of the issue we were able to find what actually is causing it and a fix has been applied. The fix should be available with our next minor release. Unfortunately, currently we cannot suggest any workaround because the issue was caused by the engine of the DataGrid.

I have logged the issue in our Feedback portal on your behalf and on the following link you can follow its status:
https://feedback.telerik.com/xamarin/1416159-datagrid-when-in-single-usersortmode-the-sortdirection-icon-of-the-templatecolumn-with-delegatesortdescriptor-is-not-cleared

I apologize for the inconvenience the issue might be causing you.

Your Telerik points have been updated as a gratitude for bringing this to our attention.

Regards,
Nasko
Progress Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Safan
Top achievements
Rank 1
answered on 27 Jun 2019, 11:41 AM

Hi Nasko,

Thanks for your reply.

Waiting for upcoming release.

Thank you.

Tags
DataGrid
Asked by
Safan
Top achievements
Rank 1
Answers by
Nasko
Telerik team
Safan
Top achievements
Rank 1
Share this question
or