Columnsort infört i lisview

This commit is contained in:
2024-02-20 18:47:01 +01:00
parent 148f6aaabf
commit 0d0373369b
2 changed files with 37 additions and 1 deletions

View File

@ -53,6 +53,7 @@
lvPayments.TabIndex = 0; lvPayments.TabIndex = 0;
lvPayments.UseCompatibleStateImageBehavior = false; lvPayments.UseCompatibleStateImageBehavior = false;
lvPayments.View = View.Details; lvPayments.View = View.Details;
lvPayments.ColumnClick += lvPayments_ColumnClick;
// //
// ch1_Id // ch1_Id
// //

View File

@ -14,6 +14,8 @@ namespace WinFormDiApp
{ {
public partial class frmPayments : Form public partial class frmPayments : Form
{ {
private ListViewColumnSorter lvwColumnSorter;
private bool _enableClearing; private bool _enableClearing;
private bool _enableDetailShow; private bool _enableDetailShow;
private readonly IAccountRecordRepository _accountRecordRepository; private readonly IAccountRecordRepository _accountRecordRepository;
@ -45,6 +47,13 @@ namespace WinFormDiApp
public frmPayments(IAccountRecordRepository accountRecordRepository) public frmPayments(IAccountRecordRepository accountRecordRepository)
{ {
InitializeComponent(); InitializeComponent();
//---------------------------------
// Create an instance of a ListView column sorter and assign it
// to the ListView control.
lvwColumnSorter = new ListViewColumnSorter();
lvPayments.ListViewItemSorter = lvwColumnSorter;
//---------------------------------
lvPayments.Items.Clear(); lvPayments.Items.Clear();
_accountRecordRepository = accountRecordRepository; _accountRecordRepository = accountRecordRepository;
} }
@ -101,7 +110,7 @@ namespace WinFormDiApp
private void btnRensa_Click(object sender, EventArgs e) private void btnRensa_Click(object sender, EventArgs e)
{ {
if(MessageBox.Show("Verkligen Säker på att rensa DB?","Payments", MessageBoxButtons.YesNo, MessageBoxIcon.Question)==DialogResult.Yes) if (MessageBox.Show("Verkligen Säker på att rensa DB?", "Payments", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{ {
MessageBox.Show("Nu rensar vi"); MessageBox.Show("Nu rensar vi");
_accountRecordRepository.DeleteAllAccountRecords(); _accountRecordRepository.DeleteAllAccountRecords();
@ -119,6 +128,32 @@ namespace WinFormDiApp
} }
} }
} }
private void lvPayments_ColumnClick(object sender, ColumnClickEventArgs e)
{
// Determine if clicked column is already the column that is being sorted.
if (e.Column == lvwColumnSorter.SortColumn)
{
// Reverse the current sort direction for this column.
if (lvwColumnSorter.Order == SortOrder.Ascending)
{
lvwColumnSorter.Order = SortOrder.Descending;
}
else
{
lvwColumnSorter.Order = SortOrder.Ascending;
}
}
else
{
// Set the column number that is to be sorted; default to ascending.
lvwColumnSorter.SortColumn = e.Column;
lvwColumnSorter.Order = SortOrder.Ascending;
}
// Perform the sort with these new sort options.
lvPayments.Sort();
}
//------------------------------------------------- sorting ------------ //------------------------------------------------- sorting ------------
//------------------------------------------------- sorting ------------ //------------------------------------------------- sorting ------------