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.
Any help would be appreciated.
Thank you!
Stefan
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