I am using ObjectListView
to display a list of items. However columns are not getting sorted when I click on the column headers.
Please refer the code pasted below:
public class Stock
{
public Stock()
{
}
public Stock(string name, double lastPrice, double greenTrend, double redTrend, double lastGreenValue, double lastRedValue)
{
this.Name = name;
this.LastPrice = lastPrice;
this.GreenTrend = greenTrend;
this.RedTrend = redTrend;
this.LastGreenValue = lastGreenValue;
this.LastRedValue = lastRedValue;
}
public string Name
{
get { return name; }
set { name = value; }
}
private string name;
public double LastPrice
{
get { return lastPrice; }
set { lastPrice = value; }
}
private double lastPrice;
public double GreenTrend
{
get { return greenTrend; }
set { greenTrend = value; }
}
private double greenTrend;
public double RedTrend
{
get { return redTrend; }
set { redTrend = value; }
}
private double redTrend;
public double LastGreenValue
{
get { return lastGreenValue; }
set { lastGreenValue = value; }
}
private double lastGreenValue;
public double LastRedValue
{
get { return lastRedValue; }
set { lastRedValue = value; }
}
private double lastRedValue;
}
Finally found the answer. When I changed the ShowGroups
property of ObjectListView
to false, sorting worked. This was set to true by default!
I made a few code changes in ObjectListView
(version 2.6.0) to enable sorting by non-groupable columns even when ShowGoups
is set to True
.
This way it's possible to have a normal sorting for columns with the Groupable
property set to False
and still be able to group the items when sorting by a column which has the Groupable
property set to True.
Make the following changes to get this behavior.
In the PostProcessRows()
method, exchange this code (around line 7729
):
if (this.ShowGroups) {
foreach (ListViewGroup group in this.Groups) {
foreach (OLVListItem olvi in group.Items) {
...
by this:
if (this.ShowGroups && ((this.LastSortColumn != null) && this.LastSortColumn.Groupable)) {
foreach (ListViewGroup group in this.Groups) {
foreach (OLVListItem olvi in group.Items) {
...
In the DoSort()
method, exchange this code (around 7391
):
if (!args.Handled) {
// Sanity checks
if (args.ColumnToSort != null && args.SortOrder != SortOrder.None) {
if (this.ShowGroups)
this.BuildGroups(args.ColumnToGroupBy, args.GroupByOrder, args.ColumnToSort, args.SortOrder,
args.SecondaryColumnToSort, args.SecondarySortOrder);
else if (this.CustomSorter != null)
this.CustomSorter(args.ColumnToSort, args.SortOrder);
else
this.ListViewItemSorter = new ColumnComparer(args.ColumnToSort, args.SortOrder,
args.SecondaryColumnToSort, args.SecondarySortOrder);
}
}
by this:
if (!args.Handled) {
// Sanity checks
if (args.ColumnToSort != null && args.SortOrder != SortOrder.None) {
if (this.ShowGroups && args.ColumnToGroupBy.Groupable)
this.BuildGroups(args.ColumnToGroupBy, args.GroupByOrder, args.ColumnToSort, args.SortOrder,
args.SecondaryColumnToSort, args.SecondarySortOrder);
else if (this.CustomSorter != null)
this.CustomSorter(args.ColumnToSort, args.SortOrder);
else
{
this.Groups.Clear();
this.ListViewItemSorter = new ColumnComparer(args.ColumnToSort, args.SortOrder,
args.SecondaryColumnToSort, args.SecondarySortOrder);
}
}
}
Now it'll be possible to sort regular and groupable columns all in one ObjectListView
combined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With