Sorting Items

13 posts, 0 answers
  1. Pawz
    Pawz avatar
    51 posts
    Member since:
    Aug 2007

    Posted 18 Jan 2012 Link to this post

    I've been trying to get my listview to sort in unbound mode (where I've added several items manually) and I can't seem to get it working.

    I've tried:
    uiMasterColourList.SortDescriptors.Add(new SortDescriptor("Text", ListSortDirection.Ascending));
    uiMasterColourList.EnableSorting = true;

    Basically I'm loading a list of colours from the database and then adding them to two different groups in the listview, along with a checkbox to see if the user has already selected the colour. Works great, but the colours end up a little mixed up and it'd be nice to sort them alphabetically inside their groups.

    So this seems like a fairly simple problem, but I seem to be missing something that's making it not work.
  2. Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 23 Jan 2012 Link to this post

    Hi Pawz,

    Thank you for writing.

    The code you have posted is the correct way to sort the items in ascending order. Also, I have tested it on my end with several manually added items and it works correctly. I am not sure what is causing this issue in your case. Generally, if you are using RadListView in bound mode, you should give the sort descriptor the property name of the bound object according to which you would like to sort. Nevertheless, I would like to kindly ask you to open a new support ticket and send me a sample project which demonstrates your scenario. This will let me investigate it and provide you with further support.

    Looking forward to hearing from you.

    Kind regards,
    Ivan Todorov
    the Telerik team

    SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Pawz
    Pawz avatar
    51 posts
    Member since:
    Aug 2007

    Posted 23 Jan 2012 Link to this post

    Hmm well maybe it's the combination of manually added groups with manually added items? Not sure. I was thinking that using "Text" in the sort descriptor propertyname was perhaps the issue...?

    Here is a sample of code that illustrates the issue. To use this, create a windows forms application, and add a RadListView control to Form1 called 'uiColourList', and replace the form1.cs with the code below.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Telerik.WinControls.Data;
    using Telerik.WinControls.Enumerations;
    using Telerik.WinControls.UI;
     
    namespace TelerikListView
    {
        public partial class Form1 : Form
        {
            private List<Colour> colours = new List<Colour>();
            private List<Colour> selectedColours = new List<Colour>();
            public Form1()
            {
                InitializeComponent();
                setupColourList();
            }
     
            private void setupColourList()
            {
               colours.Add(new Colour("Colour 1",true, false));
               colours.Add(new Colour("Colour 5",true, false));
               colours.Add(new Colour("Colour 2",true, false));
               colours.Add(new Colour("Colour 3",true, false));
               colours.Add(new Colour("Colour 7",true, false));
               colours.Add(new Colour("Colour 6",true, false));
               colours.Add(new Colour("Colour 4", false, false));
               colours.Add(new Colour("Colour 8", false, false));
               colours.Add(new Colour("Colour 9", false, false));
               colours.Add(new Colour("Colour A",false, false));
               colours.Add(new Colour("Colour C",false, false));
               colours.Add(new Colour("Colour D",false, true));
               colours.Add(new Colour("Colour E",false, true));
               colours.Add(new Colour("Colour F",false, true));
               colours.Add(new Colour("Colour B",false, true));
     
                selectedColours.Add(new Colour("Colour 1", true, false));
                selectedColours.Add(new Colour("Colour F", false, true));
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
                uiColourList.Items.Clear();
                uiColourList.Groups.Clear();
                uiColourList.EnableGrouping = true;
                uiColourList.EnableSorting = true;
                uiColourList.ShowCheckBoxes = true;
                uiColourList.ShowGroups = true;
      
                var availableColoursGroup = new ListViewDataItemGroup("Available Colours");
                var unavailableColoursGroup = new ListViewDataItemGroup("Unavailable Colours");
                var deletedColoursGroup = new ListViewDataItemGroup("Deleted Colours");
                availableColoursGroup.Text = "Available Colours";
                unavailableColoursGroup.Text = "Unavailable Colours";
                deletedColoursGroup.Text = "Deleted Colours";
                uiColourList.Groups.Add(availableColoursGroup);
                uiColourList.Groups.Add(unavailableColoursGroup);
                uiColourList.Groups.Add(deletedColoursGroup);
     
                foreach (var masterColour in colours)
                {
                    var item = new ListViewDataItem(masterColour.Name);
                    item.Tag = masterColour;
                    if (selectedColours.Any(m => m.Name == masterColour.Name))
                        item.CheckState = ToggleState.On;
                    if (!masterColour.Available)
                        item.Group = unavailableColoursGroup;
                    else
                    {
                        item.Group = availableColoursGroup;
                    }
                    if(masterColour.Deleted)
                    {
                        item.Group = deletedColoursGroup;
     
                    }
                    uiColourList.Items.Add(item);
                }
     
                uiColourList.EnableCustomGrouping = true;
                uiColourList.SortDescriptors.Add(new SortDescriptor("Text", ListSortDirection.Ascending));
                uiColourList.EnableSorting = true;
            }
         
        }
     
        public class Colour
        {
            public bool Deleted { get; set; }
            public string Name { get; set; }
            public bool Available { get; set; }
            public Colour(string name, bool available, bool deleted)
            {
                Name = name;
                Available = available;
                Deleted = deleted;
            }
        }
    }
  5. Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 26 Jan 2012 Link to this post

    Hi Pawz,

    Thank you for your assistance.

    It appears that there is an issue in RadListView which prevents the items in custom groups from being sorted. I have logged it in PITS and we will address it in a future release. Here you can find the PITS item.

    For the time being, I can suggest assigning the groups after the items have been added. This will ensure that the items are added in ascending order to their groups:
    private void Form1_Load(object sender, EventArgs e)
    {
        uiColourList.Items.Clear();
        uiColourList.Groups.Clear();
     
        uiColourList.EnableSorting = true;
        uiColourList.ShowCheckBoxes = true;
     
        uiColourList.EnableCustomGrouping = true;
        uiColourList.ShowGroups = true;
     
        uiColourList.SortDescriptors.Add(new SortDescriptor("Value", ListSortDirection.Ascending));
     
        var availableColoursGroup = new ListViewDataItemGroup("Available Colours");
        var unavailableColoursGroup = new ListViewDataItemGroup("Unavailable Colours");
        var deletedColoursGroup = new ListViewDataItemGroup("Deleted Colours");
     
        availableColoursGroup.Text = "Available Colours";
        unavailableColoursGroup.Text = "Unavailable Colours";
        deletedColoursGroup.Text = "Deleted Colours";
     
        uiColourList.Groups.Add(availableColoursGroup);
        uiColourList.Groups.Add(unavailableColoursGroup);
        uiColourList.Groups.Add(deletedColoursGroup);
     
        foreach (var masterColour in colours)
        {
            var item = new ListViewDataItem(masterColour.Name);
            item.Tag = masterColour;
     
            if (selectedColours.Any(m => m.Name == masterColour.Name))
                item.CheckState = ToggleState.On;
              
            uiColourList.Items.Add(item);
        }
     
        foreach (var item in uiColourList.Items)
        {
            Colour masterColour = item.Tag as Colour;
     
            if (!masterColour.Available)
                item.Group = unavailableColoursGroup;
            else
            {
                item.Group = availableColoursGroup;
            }
            if (masterColour.Deleted)
            {
                item.Group = deletedColoursGroup;
            }
        }
    }

    Your Telerik points have been updated for bringing this issue to our attention.

    Please let me know if the workaround does not suit your case or if you have any additional questions.

    Regards,
    Ivan Todorov
    the Telerik team

    SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

  6. Jeff
    Jeff avatar
    6 posts
    Member since:
    Jun 2016

    Posted 01 Jun in reply to Ivan Todorov Link to this post

    Hi, I'm using the radlistView with custom grouping to group my items. However as notice above, we can't sort the items while grouped. I can't use the work arround mentionned (assigning the group after all the items have been added) because, in my app, items may be added at anytime during the lifetime of the application. Does the issue have been resolved (the PITS item link redirect nowhere) or there's any other work arround?

     

    Thank you,

  7. Jeff
    Jeff avatar
    6 posts
    Member since:
    Jun 2016

    Posted 01 Jun in reply to Jeff Link to this post

    Here's a short example showing the issue :

     

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Telerik.WinControls.Data;
    using Telerik.WinControls.UI;
     
    namespace RadListViewTestApp
    {
        public partial class Form1 : Form
        {
            int _itemCpt = 100;
     
            ListViewDataItemGroup _lstGroupA = new ListViewDataItemGroup("Server 1 - 192.168.1.60");
            ListViewDataItemGroup _lstGroupB = new ListViewDataItemGroup("Server 2 - 192.168.1.61");
            ListViewDataItemGroup _lstGroupC = new ListViewDataItemGroup("Server 3 - 192.168.1.62");
     
            public Form1()
            {
                InitializeComponent();
     
                radListView1.EnableCustomGrouping = true;
                this.radListView1.ShowGroups = true;
                this.radListView1.EnableSorting = true;
                this.radListView1.SortDescriptors.Add(new SortDescriptor("Value", ListSortDirection.Ascending));
            }
     
            private void cmdAddItem_Click(object sender, EventArgs e)
            {
                _itemCpt--;
     
                AddRadListViewItem();
            }
     
            private void AddRadListViewItem()
            {
                var name = string.Format("Item {0}", _itemCpt);
                var item = new ListViewDataItem(name);
                item.Text = name;
                item.Value = name;
                item.Group = _itemCpt % 2 != 0 ? _lstGroupA : _itemCpt % 6 == 0 ? _lstGroupC : _lstGroupB;
     
                if (!radListView1.Groups.Where(group => group.Value == item.Group.Value).Any())
                    radListView1.Groups.Add(item.Group);
     
                radListView1.Items.Add(item);
            }
     
            private void cmdToggleGrouping_Click(object sender, EventArgs e)
            {
                radListView1.EnableCustomGrouping = !radListView1.EnableCustomGrouping;
                cmdToggleGrouping.Text = radListView1.EnableCustomGrouping ? "Deactivate grouping" : "Activate grouping";
            }
        }
    }

  8. Hristo
    Admin
    Hristo avatar
    716 posts

    Posted 06 Jun Link to this post

    Hello Jeff,

    Thank you for writing.

    The issue is fixed and here is the updated link to the public feedback item: FIX. Sorting in RadListView does not work when using custom grouping.

    If I understand correctly you are experiencing an issue with duplicated groups when you add items while your list view has its EnableCustomGrouping property set to false. This is caused by the method responsible for adding the items. In order to achieve the desired behavior, you would need to cache the old groups and assign a group to the newly created items from this collection. Please check below: 
    public partial class Form1 : Form
    {
        private ListViewDataItemGroup[] groupsCache;
     
        int _itemCpt = 100;
     
        ListViewDataItemGroup _lstGroupA = new ListViewDataItemGroup("Server 1 - 192.168.1.60");
        ListViewDataItemGroup _lstGroupB = new ListViewDataItemGroup("Server 2 - 192.168.1.61");
        ListViewDataItemGroup _lstGroupC = new ListViewDataItemGroup("Server 3 - 192.168.1.62");
     
        public Form1()
        {
            InitializeComponent();
     
            this.groupsCache = new ListViewDataItemGroup[0];
     
            this.radListView1.EnableCustomGrouping = true;
            this.radListView1.ShowGroups = true;
            this.radListView1.EnableSorting = true;
            this.radListView1.SortDescriptors.Add(new SortDescriptor("Value", ListSortDirection.Descending));
        }
     
        private void cmdAddItem_Click(object sender, EventArgs e)
        {
            _itemCpt--;
     
            AddRadListViewItem();
        }
     
        private void AddRadListViewItem()
        {
            var name = string.Format("Item {0}", _itemCpt);
            var item = new ListViewDataItem(name);
            item.Text = name;
            item.Value = name;
            item.Group = _itemCpt % 2 != 0 ? _lstGroupA : _itemCpt % 6 == 0 ? _lstGroupC : _lstGroupB;
     
            if (this.radListView1.EnableCustomGrouping && !radListView1.Groups.Where(group => group.Value == item.Group.Value).Any())
            {
                this.radListView1.Groups.Add(item.Group);
            }
            else if (!radListView1.EnableCustomGrouping && !this.groupsCache.Where(group => group.Value == item.Group.Value).Any())
            {
                this.radListView1.Groups.Add(item.Group);
            }
     
            radListView1.Items.Add(item);
        }
         
        private void cmdToggleGrouping_Click(object sender, EventArgs e)
        {
            if (radListView1.EnableCustomGrouping)
            {
                this.groupsCache = new ListViewDataItemGroup[this.radListView1.Groups.Count];
                this.radListView1.Groups.CopyTo(this.groupsCache, 0);
            }
     
            radListView1.EnableCustomGrouping = !radListView1.EnableCustomGrouping;
            cmdToggleGrouping.Text = radListView1.EnableCustomGrouping ? "Deactivate grouping" : "Activate grouping";
        }
    }

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  9. Jeff
    Jeff avatar
    6 posts
    Member since:
    Jun 2016

    Posted 06 Jun in reply to Hristo Link to this post

    Hi Hristo,

    Thanks for the link about sorting the radListView with custom grouping. As you said, it seem corrected. However, when I do execute my current code, the items within the group are not sorted. I have added the following lines in constructor to sort the group but didn't work either :

    GroupDescriptor groupByValue = new GroupDescriptor(new SortDescriptor[] { new SortDescriptor("Value", ListSortDirection.Ascending) });
    this.radListView1.GroupDescriptors.Add(groupByValue);

    There's a method for sorting items within the group that I should call? I use telerik dlls version 2016.1.112.40

    Thank you!

    J-F

  10. Hristo
    Admin
    Hristo avatar
    716 posts

    Posted 09 Jun Link to this post

    Hello Jeff,

    Thank you for writing.

    Could you please try adding the sort descriptor after you have added the first item in RadListView. Please check my code snippet below: 
    private void AddRadListViewItem()
    {
        var name = string.Format("Item {0}", _itemCpt);
        var item = new ListViewDataItem(name);
        item.Text = name;
        item.Value = name;
        item.Group = _itemCpt % 2 != 0 ? _lstGroupA : _itemCpt % 6 == 0 ? _lstGroupC : _lstGroupB;
     
        if (this.radListView1.EnableCustomGrouping && !radListView1.Groups.Where(group => group.Value == item.Group.Value).Any())
        {
            this.radListView1.Groups.Add(item.Group);
        }
        else if (!radListView1.EnableCustomGrouping && !this.groupsCache.Where(group => group.Value == item.Group.Value).Any())
        {
            this.radListView1.Groups.Add(item.Group);
        }
     
        this.radListView1.Items.Add(item);
     
        if (this.radListView1.SortDescriptors.Count == 0)
        {
            this.radListView1.SortDescriptors.Add(new SortDescriptor("Value", ListSortDirection.Ascending));
        }
    }

    I am also sending you a short video showing the result on  my end.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  11. Jeff
    Jeff avatar
    6 posts
    Member since:
    Jun 2016

    Posted 09 Jun in reply to Hristo Link to this post

    Thanks Hristo,

    It's now working as expected. As you notice, I must add the sort descriptor when there's at least one element.

    Have a nice day and thank you for quick reply,

  12. Hristo
    Admin
    Hristo avatar
    716 posts

    Posted 09 Jun Link to this post

    Hello Jeff,

    I am glad that now RadListView is being sorted as expected on your side.

    Please let me know if you need further assistance on that matter.

    Regards,
    Hristo Merdjanov
    Telerik
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  13. Jeff
    Jeff avatar
    6 posts
    Member since:
    Jun 2016

    Posted 09 Jun in reply to Hristo Link to this post

    One last question,
    I was expecting that the group itself would automatically be ordered. If I change the name of the group in the previous code as follow :

    ListViewDataItemGroup _lstGroupA = new ListViewDataItemGroup("Server 1 - 192.168.1.60");
    ListViewDataItemGroup _lstGroupB = new ListViewDataItemGroup("Server 4 - 192.168.1.61");
    ListViewDataItemGroup _lstGroupC = new ListViewDataItemGroup("Server 3 - 192.168.1.62");

    When server 3 is added, the group listing isn't reordered so they appear in the same order they were added (Server 1, Server4 and then Server 3). Is the EnableCustomGrouping = true;  disable this feature?

    Thanks

  14. Hristo
    Admin
    Hristo avatar
    716 posts

    Posted 10 Jun Link to this post

    Hi Jeff,

    Thank you for writing.

    I am not sure that I quite well understand your current setup. Could you please open up a support ticket and send us your project.

    Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms. For more information check out this blog post and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready