/// <summary> /// Saves the selected rows. /// </summary> public void SaveSelectedRows() { var firstRow = this.FirstDisplayedScrollingRowIndex; if (firstRow > 0) { _savedFirstRow = new SavedRow() { Index = firstRow, Text = this.Rows[firstRow].Cells[0].Value.ToString() }; } else { _savedFirstRow = null; } _savedSelectedRows.Clear(); foreach (DataGridViewRow row in this.SelectedRows) { _savedSelectedRows.Add(new SavedRow() { Index = row.Index, Text = this.Rows[row.Index].Cells[0].Value.ToString() }); } }
/// <summary> /// Restores the selected rows. /// </summary> public void RestoreSelectedRows() { ClearSelectedRows(); if (_savedSelectedRows.Count == 0) { return; } foreach (var savedRow in _savedSelectedRows) { if (savedRow.Index < this.Rows.Count && savedRow.Text == this.Rows[savedRow.Index].Cells[0].Value.ToString()) { this.Rows[savedRow.Index].Selected = true; } else { var matchingRow = this.Rows .Cast <DataGridViewRow>() .Where(r => this.Rows[this.Rows.IndexOf(r)].Cells[0].Value.ToString() == savedRow.Text) .OrderByDescending(r => Math.Abs(savedRow.Index - r.Index)) .ThenByDescending(r => savedRow.Index - r.Index) .FirstOrDefault(); if (matchingRow != null) { matchingRow.Selected = true; } } } _savedSelectedRows.Clear(); if (_savedFirstRow != null) { if (_savedFirstRow.Index < this.Rows.Count && _savedFirstRow.Text == this.Rows[_savedFirstRow.Index].Cells[0].Value.ToString()) { this.FirstDisplayedScrollingRowIndex = _savedFirstRow.Index; } else { var matchingFirstRow = this.Rows .Cast <DataGridViewRow>() .Where(r => this.Rows[this.Rows.IndexOf(r)].Cells[0].Value.ToString() == _savedFirstRow.Text) .OrderByDescending(r => Math.Abs(_savedFirstRow.Index - r.Index)) .ThenByDescending(r => _savedFirstRow.Index - r.Index) .FirstOrDefault(); if (matchingFirstRow != null) { this.FirstDisplayedScrollingRowIndex = matchingFirstRow.Index; } } } _savedFirstRow = null; }
/// <summary> /// Restores the selected rows. /// </summary> public void RestoreSelectedRows() { ClearSelectedRows(); if (_savedSelectedRows.Count == 0) return; foreach (var savedRow in _savedSelectedRows) { if (savedRow.Index < this.Rows.Count && savedRow.Text == this.Rows[savedRow.Index].Cells[0].Value.ToString()) { this.Rows[savedRow.Index].Selected = true; } else { var matchingRow = this.Rows .Cast<DataGridViewRow>() .Where(r => this.Rows[this.Rows.IndexOf(r)].Cells[0].Value.ToString() == savedRow.Text) .OrderByDescending(r => Math.Abs(savedRow.Index - r.Index)) .ThenByDescending(r => savedRow.Index - r.Index) .FirstOrDefault(); if (matchingRow != null) matchingRow.Selected = true; } } _savedSelectedRows.Clear(); if (_savedFirstRow != null) { if (_savedFirstRow.Index < this.Rows.Count && _savedFirstRow.Text == this.Rows[_savedFirstRow.Index].Cells[0].Value.ToString()) { this.FirstDisplayedScrollingRowIndex = _savedFirstRow.Index; } else { var matchingFirstRow = this.Rows .Cast<DataGridViewRow>() .Where(r => this.Rows[this.Rows.IndexOf(r)].Cells[0].Value.ToString() == _savedFirstRow.Text) .OrderByDescending(r => Math.Abs(_savedFirstRow.Index - r.Index)) .ThenByDescending(r => _savedFirstRow.Index - r.Index) .FirstOrDefault(); if (matchingFirstRow != null) this.FirstDisplayedScrollingRowIndex = matchingFirstRow.Index; } } _savedFirstRow = null; }