From 0d0373369b343d92dba25aae989cc96dad90b3bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Tue, 20 Feb 2024 18:47:01 +0100 Subject: [PATCH] =?UTF-8?q?Columnsort=20inf=C3=B6rt=20i=20lisview?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WinFormDi/frmPayments.Designer.cs | 1 + WinFormDi/frmPayments.cs | 37 ++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/WinFormDi/frmPayments.Designer.cs b/WinFormDi/frmPayments.Designer.cs index 17b2761..d8a593a 100644 --- a/WinFormDi/frmPayments.Designer.cs +++ b/WinFormDi/frmPayments.Designer.cs @@ -53,6 +53,7 @@ lvPayments.TabIndex = 0; lvPayments.UseCompatibleStateImageBehavior = false; lvPayments.View = View.Details; + lvPayments.ColumnClick += lvPayments_ColumnClick; // // ch1_Id // diff --git a/WinFormDi/frmPayments.cs b/WinFormDi/frmPayments.cs index a300574..31005ba 100644 --- a/WinFormDi/frmPayments.cs +++ b/WinFormDi/frmPayments.cs @@ -14,6 +14,8 @@ namespace WinFormDiApp { public partial class frmPayments : Form { + private ListViewColumnSorter lvwColumnSorter; + private bool _enableClearing; private bool _enableDetailShow; private readonly IAccountRecordRepository _accountRecordRepository; @@ -45,6 +47,13 @@ namespace WinFormDiApp public frmPayments(IAccountRecordRepository accountRecordRepository) { 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(); _accountRecordRepository = accountRecordRepository; } @@ -101,7 +110,7 @@ namespace WinFormDiApp 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"); _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 ------------