Custom Sorting a grid based on numerical values

2 Answers 93 Views
GridView
pierre-jean
Top achievements
Rank 1
Veteran
Iron
pierre-jean asked on 07 May 2021, 04:45 PM

Hello

I have a databound grid that has a column containings integer numerical values as per the following example:

  • 5
  • 3
  • -1
  • 2
  • -1
  • 1
  • 4

and I wish to sort this grid following the following rule:
for ascending sort I wish to have:

  • 1
  • 2
  • 3
  • 4
  • 5
  • -1
  • -1

and for descending sort:

  • 5
  • 4
  • 3
  • 2
  • 1
  • -1
  • -1

In other words I wish to have the negativ values always at the end and the positiv values at the top in ascending or descending order.

I have looked at the custom sorting documentation but I do not understand how to set the e.sortresult property to achieve this sort behavior

Thanks in advance
Best Regards
Pierre-Jean

 

 

2 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 10 May 2021, 07:47 AM

Hello, Pierre-Jean,

Indeed, the custom sorting functionality that RadGridView offers is the suitable approach if you want to control how the rows are ordered in the grid. The following help article gives more information how to use this functionality: https://docs.telerik.com/devtools/winforms/controls/gridview/sorting/custom-sorting 

The CustomSorting event is fired when two rows has to be compared. The SortResult argument should be set to 1, 0, -1 which controls which row before which other row is displayed. Return negative value when Row1 is before Row2, positive value if Row1 is after Row2 and zero if the rows are have equal values in a specified column.

I have prepared a sample code snippet for your reference:

 

        public RadForm1()
        {
            InitializeComponent();

            GridViewDecimalColumn col = new GridViewDecimalColumn("Id");
            this.radGridView1.Columns.Add(col);
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

            this.radGridView1.Rows.Add(5);
            this.radGridView1.Rows.Add(3);
            this.radGridView1.Rows.Add(-1);
            this.radGridView1.Rows.Add(2);
            this.radGridView1.Rows.Add(-1);
            this.radGridView1.Rows.Add(1);
            this.radGridView1.Rows.Add(4);

            this.radGridView1.EnableCustomSorting = true;
            this.radGridView1.CustomSorting += radGridView1_CustomSorting;
        }

        private void radGridView1_CustomSorting(object sender, GridViewCustomSortingEventArgs e)
        { 
            decimal row1Value = (decimal)e.Row1.Cells["Id"].Value;
            decimal row2Value = (decimal)e.Row2.Cells["Id"].Value;
            if (row1Value < 0 && row2Value > 0)
            {
                e.SortResult = 1;
            }
            else if (row2Value < 0 && row1Value > 0)
            {
                e.SortResult = -1;
            }
            else if (row1Value > row2Value)
            {
                e.SortResult = 1;
            }
            else if (row1Value < row2Value)
            {
                e.SortResult = -1;
            }
            else
            {
                e.SortResult = 0;
            }

            if (e.Template.SortDescriptors[0].Direction== ListSortDirection.Descending)
            {
                e.SortResult = e.SortResult*(-1);
            }
        }

The attached gif file illustrates the achieved result. Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify and extend it in a way which suits your requirements best.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Тhe web is about to get a bit better! 

The Progress Hack-For-Good Challenge has started. Learn how to enter and make the web a worthier place: https://progress-worthyweb.devpost.com.

0
pierre-jean
Top achievements
Rank 1
Veteran
Iron
answered on 10 May 2021, 10:42 AM

Hello Dess

thanks for the answer

it's all OK and works perfectly.
The point I was missing was the value to assign to e.dortresult

Thanks again
Best Regards
Pierre-Jean

Tags
GridView
Asked by
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Share this question
or