Skip to content

Commit

Permalink
Fix loss of scrolling position and selectio
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-manuel committed Nov 4, 2023
1 parent cfe9deb commit a8ed34a
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions MiniserverForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ private void RefreshGridView()
_msFolder = new List<MsFileInfo>();
}

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

// Save the current selection
var selectedCells = _dataGridView.SelectedCells.Cast<DataGridViewCell>()
.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);

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a8ed34a

Please sign in to comment.