I use these codes to re-order columns in a ListView
in ascending or descending order:
public static ObservableCollection<Inventory> SortedDailyCollection = new ObservableCollection<Inventory>();
public static ObservableCollection<Inventory> SortedCustomCollection = new ObservableCollection<Inventory>();
bool isDaily = true;
bool descAsc = true;
bool suppAsc = true;
bool soldAsc = true;
private void lvProductsColumnHeader_Click(object sender, RoutedEventArgs e)
{
GridViewColumnHeader column = (sender as GridViewColumnHeader);
string sortBy = column.Tag.ToString();
if (listViewSortCol != null)
{
AdornerLayer.GetAdornerLayer(listViewSortCol).Remove(listViewSortAdorner);
}
ListSortDirection newDir = ListSortDirection.Ascending;
if (listViewSortCol == column && listViewSortAdorner.Direction == newDir)
newDir = ListSortDirection.Descending;
listViewSortCol = column;
listViewSortAdorner = new SortAdorner(listViewSortCol, newDir);
AdornerLayer.GetAdornerLayer(listViewSortCol).Add(listViewSortAdorner);
switch (sortBy)
{
case "Description":
SortDescription();
break;
case "Supplier":
SortSupplier();
break;
case "Sold":
SortSold();
break;
default:
break;
}
}
private void SortDescription()
{
ObservableCollection<Inventory> chosenCollection = (isDaily) ? SortedDailyCollection : SortedCustomCollection;
List<Inventory> InventoryList = chosenCollection.ToList();
InventoryList = (descAsc) ? InventoryList = InventoryList.OrderBy(r => r.ProductDescription).ToList() : InventoryList = InventoryList.OrderByDescending(r => r.ProductDescription).ToList();
var tempCollection = new ObservableCollection<Inventory>(InventoryList);
chosenCollection.Clear();
foreach (var item in tempCollection)
{
Inventory _inventory = new Inventory
{
ProductDescription = item.ProductDescription,
Supplier = item.Supplier,
ProductSold = item.ProductSold,
ProductCost = item.ProductCost,
ProductRetail = item.ProductRetail,
TotalRetail = item.TotalRetail
};
chosenCollection.Add(_inventory);
}
listView.ItemsSource = chosenCollection;
if (descAsc)
{
descAsc = false;
suppAsc = true;
soldAsc = true;
}
else
{
descAsc = true;
}
}
private void SortSupplier()
{
ObservableCollection<Inventory> chosenCollection = (isDaily) ? SortedDailyCollection : SortedCustomCollection;
List<Inventory> InventoryList = chosenCollection.ToList();
InventoryList = (suppAsc) ? InventoryList = InventoryList.OrderBy(r => r.Supplier).ToList() : InventoryList = InventoryList.OrderByDescending(r => r.Supplier).ToList();
var tempCollection = new ObservableCollection<Inventory>(InventoryList);
chosenCollection.Clear();
foreach (var item in tempCollection)
{
Inventory _inventory = new Inventory
{
ProductDescription = item.ProductDescription,
Supplier = item.Supplier,
ProductSold = item.ProductSold,
ProductCost = item.ProductCost,
ProductRetail = item.ProductRetail,
TotalRetail = item.TotalRetail
};
chosenCollection.Add(_inventory);
}
listView.ItemsSource = chosenCollection;
if (suppAsc)
{
descAsc = true;
suppAsc = false;
soldAsc = true;
}
else
{
suppAsc = true;
}
}
private void SortSold()
{
ObservableCollection<Inventory> chosenCollection = (isDaily) ? SortedDailyCollection : SortedCustomCollection;
List<Inventory> InventoryList = chosenCollection.ToList();
InventoryList = (soldAsc) ? InventoryList = InventoryList.OrderByDescending(r => r.ProductSold).ToList() : InventoryList = InventoryList.OrderBy(r => r.ProductSold).ToList();
var tempCollection = new ObservableCollection<Inventory>(InventoryList);
chosenCollection.Clear();
foreach (var item in tempCollection)
{
Inventory _inventory = new Inventory
{
ProductDescription = item.ProductDescription,
Supplier = item.Supplier,
ProductSold = item.ProductSold,
ProductCost = item.ProductCost,
ProductRetail = item.ProductRetail,
TotalRetail = item.TotalRetail
};
chosenCollection.Add(_inventory);
}
listView.ItemsSource = chosenCollection;
if (soldAsc)
{
descAsc = true;
suppAsc = true;
soldAsc = false;
}
else
{
soldAsc = true;
}
}
Is there a way to reduce the booleans
to be used? There will be times that the columns will increase or decrease, thus the change in boolean
count. I went with this method so that I can check whether the same column is clicked again or not.
Note: The isDaily
changes based from date, but is not included here.