如果您想根据需要向上/向下移动选定的行,您可以使用以下代码移动:
向上:
if (dataGridView1.SelectedRows[0].Index != 0) {
for (int j = 0; j < this.dataGridView1.Columns.Count; j++) {
object tmp = this.dataGridView1[j, dataGridView1.SelectedRows[0].Index].Value;
this.dataGridView1[j, dataGridView1.SelectedRows[0].Index ].Value = this.dataGridView1[j, dataGridView1.SelectedRows[0].Index - 1].Value;
this.dataGridView1[j, dataGridView1.SelectedRows[0].Index - 1].Value = tmp;
}
int a = dataGridView1.SelectedRows[0].Index;
dataGridView1.ClearSelection();
this.dataGridView1.Rows[a - 1].Selected = true;
}
向下:
if (dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 2) {
for (int j = 0; j < this.dataGridView1.Columns.Count; j++) {
object tmp = this.dataGridView1[j, dataGridView1.SelectedRows[0].Index].Value;
this.dataGridView1[j, dataGridView1.SelectedRows[0].Index].Value = this.dataGridView1[j, dataGridView1.SelectedRows[0].Index + 1].Value;
this.dataGridView1[j, dataGridView1.SelectedRows[0].Index + 1].Value = tmp;
}
int i = dataGridView1.SelectedRows[0].Index;
dataGridView1.ClearSelection();
this.dataGridView1.Rows[i + 1].Selected = true;
}