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

Custom sorting with connected rows

3 Answers 147 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Stefan
Top achievements
Rank 1
Stefan asked on 05 Jan 2011, 02:31 PM
Hi,

I'm using a GridView to display "Order Items".
Order Items can be arranged hierarchically by setting the Property "ParentOrderItemID" equal to the Property "OrderItemID" of the parent order item. Please not I can NOT use hierarchical grids.

What I want to do now is enabling custom sorting the following way:
If the User sorts by a certain attribute the sort algorithm must still keep together connected rows.

Say the grid has the following structure
[Order Item ID | Parent Order Item ID | Gross Value]

And the following values
[1 | null | 30]
  [2 | 1 | 20]
  [3 | 1 | 15]
[4 | null | 10]

If I now sort by "Gross Value", I want the sorted result to be:
[4 | null | 10]
[1 | null | 30]
  [3 | 1 | 15]
  [2 | 1 | 20]

How can I do it? I tried to use CustomSorting Event, but I'm stuck.

private void orderItems_grid_CustomSorting(object sender, GridViewCustomSortingEventArgs e)
       {
           if (this.orderItems_grid.SortDescriptors.Count == 0)
           {
               return;
           }
 
           int result = 0;
 
           SD_OrderItems item1 = e.Row1.DataBoundItem as SD_OrderItems;
           SD_OrderItems item2 = e.Row2.DataBoundItem as SD_OrderItems;
 
           for (int i = 0; i < this.orderItems_grid.SortDescriptors.Count; i++)
           {
               string cellValue1 = e.Row1.Cells[this.orderItems_grid.SortDescriptors[i].PropertyName].Value.ToString();
               string cellValue2 = e.Row2.Cells[this.orderItems_grid.SortDescriptors[i].PropertyName].Value.ToString();
 
               if (item1.ParentOrderItem.HasValue && item2.ParentOrderItem.HasValue)
               {
                   if (item1.ParentOrderItem.Value == item2.ParentOrderItem.Value)
                       result = cellValue1.CompareTo(cellValue2);
                   else
                       result = item1.ParentOrderItem.Value.CompareTo(item2.ParentOrderItem.Value);
               }
               else
               {
                   result = cellValue1.CompareTo(cellValue2);
               }
                
                
               if (result != 0)
               {
                   if (this.orderItems_grid.SortDescriptors[i].Direction == ListSortDirection.Descending)
                   {
                       result *= -1;
                   }
                   break;
               }
           }
           e.SortResult = result;
       }


Any help would be appreciated.

Thank you!
Stefan

3 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 07 Jan 2011, 10:16 AM
Hello Stefan,

Before handling the custom sorting event in you scenario you must set the EnableCustomSorting property to true. Here is the example used in our Demo application:

public partial class Form1 : ExamplesForm
{
    public Form1()
    {
        InitializeComponent();
         
        this.radGridView.EnableCustomSorting = true;
    }
 
    private void OnFormLoad(object sender, EventArgs e)
    {
        this.customersTableAdapter.Fill(this.nwindRadGridView.Customers);
        this.SelectedControl = this.radGridView;
    }
 
    private void radGridView1_CustomSorting(object sender, GridViewCustomSortingEventArgs e)
    {
        if (this.radGridView.SortDescriptors.Count == 0)
        {
            return;
        }
 
        int result = 0;
        for (int i = 0; i < this.radGridView.SortDescriptors.Count; i++)
        {
            string cellValue1 = e.Row1.Cells[this.radGridView.SortDescriptors[i].PropertyName].Value.ToString();
            string cellValue2 = e.Row2.Cells[this.radGridView.SortDescriptors[i].PropertyName].Value.ToString();
 
            result = cellValue1.Length - cellValue2.Length;
            if (result != 0)
            {
                if (this.radGridView.SortDescriptors[i].Direction == ListSortDirection.Descending)
                {
                    result *= -1;
                }
                break;
            }
        }
 
        e.SortResult = result;
    }
}

I hope this helps.

Greetings,
Julian Benkov
the Telerik team
Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
0
Stefan
Top achievements
Rank 1
answered on 10 Jan 2011, 10:01 AM
Hi Julian,

thanks for your post, but this does not answer my question.
As you can see in my question I do know how to handle (and enable) custom sorting. My problem is to keep connected rows together.

Best,
Stefan
0
Julian Benkov
Telerik team
answered on 13 Jan 2011, 11:22 AM
Hello Stefan,

In order to keep related rows together using their parent value, the sorting operation first must sort the parent values and then you can perform sorting using additional sorting descriptors. Here is an example using your code snippet:

private void orderItems_grid_CustomSorting(object sender, GridViewCustomSortingEventArgs e)
{
    SD_OrderItems item1 = e.Row1.DataBoundItem as SD_OrderItems;
    SD_OrderItems item2 = e.Row2.DataBoundItem as SD_OrderItems;
     
    int result = item1.ParentOrderItem.Value.CompareTo(item2.ParentOrderItem.Value)
    if(result == 0)
    {
        for (int i = 0; i < this.orderItems_grid.SortDescriptors.Count; i++)
        {
            string cellValue1 = e.Row1.Cells[this.orderItems_grid.SortDescriptors[i].PropertyName].Value.ToString();
            string cellValue2 = e.Row2.Cells[this.orderItems_grid.SortDescriptors[i].PropertyName].Value.ToString();
 
            result = cellValue1.CompareTo(cellValue2);
            if (result != 0)
            {
                if (this.orderItems_grid.SortDescriptors[i].Direction == ListSortDirection.Descending)
                {
                    result *= -1;
                }
                break;
            }
        }
    }
     
    e.SortResult = result;
}

The sorted result will be:

[null | 4 | 10]
[null | 1 | 30]
[1 | 3 | 15] 
[1 | 2 | 20]


Regards,
Julian Benkov
the Telerik team
Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
Tags
GridView
Asked by
Stefan
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Stefan
Top achievements
Rank 1
Share this question
or