From 643e53b575fd1869393fa76cf313a701c788fc98 Mon Sep 17 00:00:00 2001 From: tznind Date: Tue, 22 Nov 2022 10:57:03 +0000 Subject: [PATCH] Add column sorting to TableEditor --- UICatalog/Scenarios/TableEditor.cs | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/UICatalog/Scenarios/TableEditor.cs b/UICatalog/Scenarios/TableEditor.cs index 2f71e30ae..7e14d11c3 100644 --- a/UICatalog/Scenarios/TableEditor.cs +++ b/UICatalog/Scenarios/TableEditor.cs @@ -130,6 +130,50 @@ namespace UICatalog.Scenarios { Focus = Win.ColorScheme.Focus, Normal = Application.Driver.MakeAttribute(Color.Red,Color.BrightBlue) }; + + // if user clicks the mouse in TableView + tableView.MouseClick += e => { + + tableView.ScreenToCell (e.MouseEvent.X, e.MouseEvent.Y, out DataColumn clickedCol); + + if (clickedCol != null) { + + // work out new sort order + var sort = tableView.Table.DefaultView.Sort; + bool isAsc; + + if(sort?.EndsWith("ASC") ?? false) { + sort = $"{clickedCol.ColumnName} DESC"; + isAsc = false; + } else { + sort = $"{clickedCol.ColumnName} ASC"; + isAsc = true; + } + + // set a sort order + tableView.Table.DefaultView.Sort = sort; + + // copy the rows from the view + var sortedCopy = tableView.Table.DefaultView.ToTable (); + tableView.Table.Rows.Clear (); + foreach(DataRow r in sortedCopy.Rows) { + tableView.Table.ImportRow (r); + } + + foreach(DataColumn col in tableView.Table.Columns) { + + // remove any lingering sort indicator + col.ColumnName = col.ColumnName.TrimEnd ('▼', '▲'); + + // add a new one if this the one that is being sorted + if (col == clickedCol) { + col.ColumnName += isAsc ? '▲': '▼'; + } + } + + tableView.Update (); + } + }; }