/winforms-grid-move-focused-row-up-down

Swap the focused grid row and its adjacent row (top or bottom).

Primary LanguageC#OtherNOASSERTION

WinForms Data Grid - How to move data rows up and down

This example shows how to swap the focused row and its adjacent row (top or bottom).

// Moves the focused row up.
private void button1_Click(object sender, System.EventArgs e) {
	DevExpress.XtraGrid.Views.Grid.GridView view = gridView1;
	view.GridControl.Focus();
	int index = view.FocusedRowHandle;
	if(index <= 0) return;
	DataRow row1 = view.GetDataRow(index);
	DataRow row2 = view.GetDataRow(index - 1);
	object val1 = row1[OrderFieldName];
	object val2 = row2[OrderFieldName];
	row1[OrderFieldName] = val2;
	row2[OrderFieldName] = val1;
	view.FocusedRowHandle = index - 1;
}
// Moves the focused row down.
private void button2_Click(object sender, System.EventArgs e) {
	DevExpress.XtraGrid.Views.Grid.GridView view = gridView1;
	view.GridControl.Focus();
	int index = view.FocusedRowHandle;
	if(index >= view.DataRowCount - 1) return;
	DataRow row1 = view.GetDataRow(index);
	DataRow row2 = view.GetDataRow(index + 1);
	object val1 = row1[OrderFieldName];
	object val2 = row2[OrderFieldName];
	row1[OrderFieldName] = val2;
	row2[OrderFieldName] = val1;
	view.FocusedRowHandle = index + 1;
}

Files to Review

See Also

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)