From a8ed34ac7051ac99a29c8ba0f7b9e3e8d907d09a Mon Sep 17 00:00:00 2001 From: Manuel Date: Sat, 4 Nov 2023 22:22:22 +0100 Subject: [PATCH] Fix loss of scrolling position and selectio --- MiniserverForm.cs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/MiniserverForm.cs b/MiniserverForm.cs index b5bb324..faef9a1 100644 --- a/MiniserverForm.cs +++ b/MiniserverForm.cs @@ -130,6 +130,15 @@ private void RefreshGridView() _msFolder = new List(); } + // Save the current scrolling position + int scrollPosition = _dataGridView.FirstDisplayedScrollingRowIndex; + Console.WriteLine($"Scroll position: {scrollPosition}"); + + // Save the current selection + var selectedCells = _dataGridView.SelectedCells.Cast() + .Select(cell => new { cell.RowIndex, cell.ColumnIndex }) + .ToList(); + var msMap = _msFolder.ToLookup(e => e.FileName, StringComparer.OrdinalIgnoreCase); var localMap = _localFolder.ToLookup(e => e.Name, StringComparer.OrdinalIgnoreCase); @@ -200,6 +209,27 @@ private void RefreshGridView() // Bind the SortableBindingList to the DataGridView _dataGridView.DataSource = _fileItems; + + // Restore the scrolling position + if (_dataGridView.RowCount > 0 && scrollPosition != -1 && scrollPosition < _dataGridView.RowCount) + { + _dataGridView.FirstDisplayedScrollingRowIndex = scrollPosition; + } + + // Clear the default selection + _dataGridView.ClearSelection(); + + // Restore the selection + if (selectedCells.Any()) + { + foreach (var cell in selectedCells) + { + if (cell.RowIndex < _dataGridView.RowCount && cell.ColumnIndex < _dataGridView.ColumnCount) + { + _dataGridView.Rows[cell.RowIndex].Cells[cell.ColumnIndex].Selected = true; + } + } + } } private void RefreshMs()