Hi I need to apply "None" as ListSortDirection on a Grouping column programmatically but only "Ascending" and "Descending" values are possible.
Any idea of how perform this action?
Francisco
GroupDescriptor groupByTemperature =
new
GroupDescriptor();
groupByTemperature.GroupNames.Add(
"Temperature"
, ListSortDirection.Ascending);
Any idea of how perform this action?
Francisco
3 Answers, 1 is accepted
0
Hello Francisco,
Thank you for writing.
The GroupDescriptor.GroupNames property defines the property, by which the data will be grouped. The GroupName is a SortDescriptorCollection and defines group names for one grouping criteria. Note that System.ComponentModel.ListSortDirection enumeration specifies the direction of a sort operation. It has only two values: Ascending and Descending.
However, to change the order of your Groups, you must implement your own generic group comparer and change the default one. Here is a simple example:
I hope this information helps. Should you have further questions, I would be glad to help.
Regards,
Dess
Telerik
Thank you for writing.
The GroupDescriptor.GroupNames property defines the property, by which the data will be grouped. The GroupName is a SortDescriptorCollection and defines group names for one grouping criteria. Note that System.ComponentModel.ListSortDirection enumeration specifies the direction of a sort operation. It has only two values: Ascending and Descending.
However, to change the order of your Groups, you must implement your own generic group comparer and change the default one. Here is a simple example:
private
void
Form1_Load(
object
sender, EventArgs e)
{
this
.customersTableAdapter.Fill(
this
.nwindDataSet.Customers);
this
.radGridView1.MasterTemplate.GroupComparer =
new
GroupComparer();
GroupDescriptor descriptor =
new
GroupDescriptor();
descriptor.GroupNames.Add(
"Country"
, ListSortDirection.Ascending);
this
.radGridView1.GroupDescriptors.Add(descriptor);
}
public
class
GroupComparer : IComparer<Group<GridViewRowInfo>>
{
public
int
Compare(Group<GridViewRowInfo> x, Group<GridViewRowInfo> y)
{
return
x.Key.GetHashCode().CompareTo(y.Key.GetHashCode());
}
}
I hope this information helps. Should you have further questions, I would be glad to help.
Dess
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
0
FMorales
Top achievements
Rank 1
answered on 23 Feb 2015, 08:14 AM
Hi Dess,
thanks you very much for your reply.
i am trying to create a groupComparer for more than one level but I can not get it works, could you provide a simple example of a Compare method for more than one level??, I notice that x and y has a property level but I do not understand how make a sorting correctly.
I am trying for example to create 2 groups, one for Temperature and another one for Voltage, both with int value.
Thanks a lot
Francisco
thanks you very much for your reply.
i am trying to create a groupComparer for more than one level but I can not get it works, could you provide a simple example of a Compare method for more than one level??, I notice that x and y has a property level but I do not understand how make a sorting correctly.
I am trying for example to create 2 groups, one for Temperature and another one for Voltage, both with int value.
Thanks a lot
Francisco
0
Hello Francisco,
Thank you for writing back.
The GroupComparer is set globally to the MasterTemplate. Its Compare method will be called every time when two groups need to be compared. The Level property just indicates to what level the specific group belongs. Hence, when you add one group, you perform the desired groups ordering for that level. Afterwards, when you add another group, the Compare method will be called again for all group levels. Note that you have access to the specific GroupDescriptor and you can determine over what property/column you are going to group and perform the desired order.
Here is a sample code snippet demonstrating how to sort the groups for column "B" by considering the numeric value and order the groups in column "C" in a reversed string order:
I hope this information helps. If you have any additional questions, please let me know.
Regards,
Dess
Telerik
Thank you for writing back.
The GroupComparer is set globally to the MasterTemplate. Its Compare method will be called every time when two groups need to be compared. The Level property just indicates to what level the specific group belongs. Hence, when you add one group, you perform the desired groups ordering for that level. Afterwards, when you add another group, the Compare method will be called again for all group levels. Note that you have access to the specific GroupDescriptor and you can determine over what property/column you are going to group and perform the desired order.
Here is a sample code snippet demonstrating how to sort the groups for column "B" by considering the numeric value and order the groups in column "C" in a reversed string order:
public
Form1()
{
InitializeComponent();
DataTable dt =
new
DataTable();
dt.Columns.Add(
"A"
,
typeof
(
string
));
dt.Columns.Add(
"B"
,
typeof
(
string
));
dt.Columns.Add(
"C"
,
typeof
(
string
));
dt.Rows.Add(
"DataA.1"
,
"1"
,
"DataC.4"
);
dt.Rows.Add(
"DataA.2"
,
"5"
,
"DataC.42"
);
dt.Rows.Add(
"DataA.3"
,
"10"
,
"DataC.43"
);
dt.Rows.Add(
"DataA.4"
,
"11"
,
"DataC.14"
);
dt.Rows.Add(
"DataA.5"
,
"20"
,
"DataC.54"
);
dt.Rows.Add(
"DataA.6"
,
"2"
,
"DataC.6"
);
dt.Rows.Add(
"DataA.7"
,
"3"
,
"DataC.17"
);
dt.Rows.Add(
"DataA.1"
,
"1"
,
"DataC.1"
);
dt.Rows.Add(
"DataA.2"
,
"5"
,
"DataC.2"
);
dt.Rows.Add(
"DataA.3"
,
"10"
,
"DataC.3"
);
dt.Rows.Add(
"DataA.4"
,
"11"
,
"DataC.4"
);
dt.Rows.Add(
"DataA.5"
,
"20"
,
"DataC.5"
);
dt.Rows.Add(
"DataA.6"
,
"2"
,
"DataC.6"
);
dt.Rows.Add(
"DataA.7"
,
"3"
,
"DataC.7"
);
this
.radGridView1.DataSource = dt;
this
.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
this
.radGridView1.MasterTemplate.GroupComparer =
new
GroupComparer();
}
public
class
GroupComparer : IComparer<Group<GridViewRowInfo>>
{
public
int
Compare(Group<GridViewRowInfo> x, Group<GridViewRowInfo> y)
{
DataGroup group = x
as
DataGroup;
if
(group ==
null
)
{
group = y
as
DataGroup;
}
string
propertyName = group.GroupDescriptor.GroupNames.First().PropertyName;
if
(propertyName ==
"B"
)
{
int
parsedX;
int
parsedY;
if
(
int
.TryParse(((
object
[])x.Key).First().ToString(),
out
parsedX) &&
int
.TryParse(((
object
[])y.Key).First().ToString(),
out
parsedY))
{
if
(group.GroupDescriptor.GroupNames.First().Direction == ListSortDirection.Ascending)
{
return
parsedX.CompareTo(parsedY);
}
else
{
return
(-1) * parsedX.CompareTo(parsedY);
}
}
}
else
if
(propertyName ==
"C"
)
{
if
(group.GroupDescriptor.GroupNames.First().Direction == ListSortDirection.Ascending)
{
return
(-1) * ((
object
[])x.Key).First().ToString().CompareTo(((
object
[])y.Key).First().ToString());
}
else
{
return
((
object
[])x.Key).First().ToString().CompareTo(((
object
[])y.Key).First().ToString());
}
}
return
x.Key.ToString().CompareTo(y.Key.ToString());
}
}
I hope this information helps. If you have any additional questions, please let me know.
Regards,
Dess
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.