DataGridViewColumn oldColumn = new DataGridViewColumn();
SortOrder sortOrder = new SortOrder();
List<int> selectedItems = new List<int>();
int saveRow = 0;
SortOrder sortOrder = new SortOrder();
List<int> selectedItems = new List<int>();
int saveRow = 0;
//Aktuelle Sortierung zwischenspeichern
oldColumn = dataGridView.SortedColumn;
sortOrder = dataGridView.SortOrder;
if (dataGridView.SelectedRows.Count > 0)
{
foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
selectedItems.Add(row.Index);
}
}
//Aktuelle erste angezeigte Zeile speichern
if (dataGridView.Rows.Count > 0)
{
if ((dataGridView.FirstDisplayedCell != null))
{
saveRow = dataGridView.FirstDisplayedCell.RowIndex;
}
}
oldColumn = dataGridView.SortedColumn;
sortOrder = dataGridView.SortOrder;
if (dataGridView.SelectedRows.Count > 0)
{
foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
selectedItems.Add(row.Index);
}
}
//Aktuelle erste angezeigte Zeile speichern
if (dataGridView.Rows.Count > 0)
{
if ((dataGridView.FirstDisplayedCell != null))
{
saveRow = dataGridView.FirstDisplayedCell.RowIndex;
}
}
//Your Code
//Sortierung anwenden
if ((oldColumn != null))
{
switch (sortOrder)
{
case SortOrder.None:
break;
case SortOrder.Ascending:
dataGridView.Sort(oldColumn, ListSortDirection.Ascending);
break;
case SortOrder.Descending:
dataGridView.Sort(oldColumn, ListSortDirection.Descending);
break;
}
}
else
{
dataGridView.Sort(dataGridView.Columns[4], ListSortDirection.Ascending);
}
//Alle SelectedRows resetten
foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
row.Selected = false;
}
//SelectedRows wieder setzen
if (selectedItems.Count > 0)
{
foreach (int item in selectedItems)
{
if (dataGridView.Rows.Count > 0)
{
if (dataGridView.Rows.Count - 1 >= item)
{
dataGridView.Rows[item].Selected = true;
}
}
}
}
//Scrollbar wieder richtig setzen
if (saveRow != 0 & saveRow < dataGridView.Rows.Count)
{
dataGridView.FirstDisplayedScrollingRowIndex = saveRow;
}
if ((oldColumn != null))
{
switch (sortOrder)
{
case SortOrder.None:
break;
case SortOrder.Ascending:
dataGridView.Sort(oldColumn, ListSortDirection.Ascending);
break;
case SortOrder.Descending:
dataGridView.Sort(oldColumn, ListSortDirection.Descending);
break;
}
}
else
{
dataGridView.Sort(dataGridView.Columns[4], ListSortDirection.Ascending);
}
//Alle SelectedRows resetten
foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
row.Selected = false;
}
//SelectedRows wieder setzen
if (selectedItems.Count > 0)
{
foreach (int item in selectedItems)
{
if (dataGridView.Rows.Count > 0)
{
if (dataGridView.Rows.Count - 1 >= item)
{
dataGridView.Rows[item].Selected = true;
}
}
}
}
//Scrollbar wieder richtig setzen
if (saveRow != 0 & saveRow < dataGridView.Rows.Count)
{
dataGridView.FirstDisplayedScrollingRowIndex = saveRow;
}
Keine Kommentare:
Kommentar veröffentlichen