Skip to content

Commit

Permalink
Save sorting and selection arrow when refreshing file list
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-manuel committed Jul 17, 2024
1 parent 7de94eb commit 9a6b004
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Added: Calculate entries
* Added: Filter entries
* Added: Select related row when clicking on graph
* Changed: Save sorting and selection arrow when refreshing file list

## v1.0.4.0

Expand Down
60 changes: 58 additions & 2 deletions MiniserverForm.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -35,8 +36,27 @@ public string Name
{
try
{
if(FileInfo != null)
if (FileInfo != null)
{
_name = LoxStatFile.Load(FileInfo.FullName, true).Text;

// Append the file extension as a suffix could be _1.202407 or 202407
// This is a workaround until the sorting on two levels is implemented
// e.g. sort by description and then by filename, else the files are not in historical order
_name += " [";

// check if filename contains _
if (FileInfo.Name.Contains("_"))
{
string[] split = FileInfo.Name.Split('_');
_name += split[1];
}
else {
_name += FileInfo.Name.Substring(FileInfo.Name.Length - 6);
}

_name += "]";
}
}
catch(Exception)
{
Expand Down Expand Up @@ -130,15 +150,23 @@ private void RefreshGridView()
_msFolder = new List<MsFileInfo>();
}

// Save the current sort conditions
var sortColumn = _dataGridView.SortedColumn;
var sortOrder = _dataGridView.SortOrder;

// Save the current scrolling position
int scrollPosition = _dataGridView.FirstDisplayedScrollingRowIndex;
Console.WriteLine($"Scroll position: {scrollPosition}");
//Console.WriteLine($"Scroll position: {scrollPosition}");

// Save the current selection
var selectedCells = _dataGridView.SelectedCells.Cast<DataGridViewCell>()
.Select(cell => new { cell.RowIndex, cell.ColumnIndex })
.ToList();

// Save the current active cell (for restoring the selection arrow)
int? activeCellRowIndex = _dataGridView.CurrentCell?.RowIndex;
int? activeCellColumnIndex = _dataGridView.CurrentCell?.ColumnIndex;

var msMap = _msFolder.ToLookup(e => e.FileName, StringComparer.OrdinalIgnoreCase);
var localMap = _localFolder.ToLookup(e => e.Name, StringComparer.OrdinalIgnoreCase);

Expand Down Expand Up @@ -214,6 +242,26 @@ private void RefreshGridView()
// Bind the SortableBindingList to the DataGridView
_dataGridView.DataSource = _fileItems;

// Restore the sort conditions
if (sortColumn != null && sortOrder != SortOrder.None)
{
// Determine the ListSortDirection from the SortOrder
ListSortDirection direction = sortOrder == SortOrder.Ascending ? ListSortDirection.Ascending : ListSortDirection.Descending;

// Find the DataGridViewColumn to sort by
DataGridViewColumn columnToSort = _dataGridView.Columns
.OfType<DataGridViewColumn>()
.FirstOrDefault(c => c.DataPropertyName == sortColumn.DataPropertyName);

// TODO: Add second sort level, to always sort by FileName as second level with same direction as first level

if (columnToSort != null)
{
// Reapply the sort
_dataGridView.Sort(columnToSort, direction);
}
}

// Restore the scrolling position
if (_dataGridView.RowCount > 0 && scrollPosition != -1 && scrollPosition < _dataGridView.RowCount)
{
Expand All @@ -223,6 +271,14 @@ private void RefreshGridView()
// Clear the default selection
_dataGridView.ClearSelection();

// Restore the CurrentCell to restore the selection arrow
if (activeCellRowIndex.HasValue && activeCellColumnIndex.HasValue
&& activeCellRowIndex.Value < _dataGridView.RowCount
&& activeCellColumnIndex.Value < _dataGridView.ColumnCount)
{
_dataGridView.CurrentCell = _dataGridView.Rows[activeCellRowIndex.Value].Cells[activeCellColumnIndex.Value];
}

// Restore the selection
if (selectedCells.Any())
{
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
- Check, that if you cancel the calculation, that it is really canceled
- Calc from row: Only works if you select at least two cells. Then it calculates all values including the last cell. This should only work if you have selected one cell?
- Calc selected: Calculates all selected cells using the formula entered. Whereby the formula differs from the formula in the other calculation. If you enter V - 100 here, it will not work. If you only enter - 100 it works
- Read Loxone XML file to be able to show statistics name before downloading statistic files

0 comments on commit 9a6b004

Please sign in to comment.