Home › Forums › WinForms controls › Xceed Grid for WinForms › Move selected rows up & down
-
AuthorPosts
-
#16665 |
How to move many selected rows up or down (top or bottom) on button click?
e. g. this code works fine to move selected row (only one) by one position up:
private void moveUpRow() {
DataRowList dataRowList = this.gridControl1.DataRows;
Xceed.Grid.
DataRow currentRow = gridControl1.CurrentRow as Xceed.Grid.DataRow;
if ((currentRow != null) && (gridControl1.SortedColumns.Count == 0))
{
int index = dataRowList.IndexOf(currentRow);
if (index != 0)
{
Xceed.Grid.
DataRow previousRow = this.gridControl1.DataRows[index – 1];
this.SwapRows(currentRow, previousRow);
gridControl1.SelectedRows.Clear();
gridControl1.CurrentRow = previousRow;
gridControl1.SelectedRows.Add(previousRow);
}
}
}
Imported from legacy forums. Posted by fluxay (had 1591 views)
Well, update the SwapRows() method so it can take many rows (in fact the SelectedRows collection) and change the index on all these rows to reflect their new position, and then don’t clear the SelectedRows if you want to keep the selection.
Imported from legacy forums. Posted by André (had 297 views)
The SwapRows(currentRow, previousRow); method taken from your grid samples looks like this:
private void SwapRows(CellRow one, CellRow two)
{
for (int i = 0; i < one.Cells.Count; i++)
{
object pivot = one.Cells[ i ].Value;
one.Cells[ i ].Value = two.Cells[ i ].Value;
two.Cells[ i ].Value = pivot;
}
}
So indexes don’t change, just values in cells one by one, as I understand.
How can I access all selected rows onbuttonclick, keep them in index array and swap one by one? Is it good idea, cos
foreach (Row selectedRow in gridControl1.SelectedRows) {…} looks simple but throws InvalidOperationException if selection changes inside {…}.
I’m using unbound grid.
Imported from legacy forums. Posted by fluxay (had 676 views)
It seems that moving rows cell by cell is not exactly what I need. My third column contains progressbar and it doesn’t move correctly in such a way.
BTW: How to move rows (one or many) by mouse? RowSelectorPane is invisible.
Imported from legacy forums. Posted by fluxay (had 892 views)
You can instead add an index column, that you hide and on which the grid is sorted. Thus you simply need to change the index of the rows you want to move to another location, and the sort will make it that the rows will reposition.
Imported from legacy forums. Posted by André (had 1067 views)
-
AuthorPosts
- You must be logged in to reply to this topic.