I have a DataGridView where I have made the functions for MoveUp and MoveDown for the rows. But when I swap two rows and change the selected rows, the arrow of selected row stayed at the previous place although I set the selected row on the swapped row. Can I change the arrow on the correct row? It's just a small detail but I want to know if it's possible to do it.
I have this code:
private void moveUp()
{
if (dataGridView1.RowCount > 0)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int rowCount = dataGridView1.Rows.Count;
int index = dataGridView1.SelectedCells[0].OwningRow.Index;
if (index == 0)
{
return;
}
var temp = listApp[index];
var temp2 = listApp[index - 1];
listApp.Remove(temp);
listApp.Remove(temp2);
listApp.Insert(index-1, temp);
listApp.Insert(index, temp2);
dataGridView1.DataSource = null;
dataGridView1.DataSource = listApp;
dataGridView1.SelectedCells[0].Selected = false;
//dataGridView1.Rows[index].Selected = false;
dataGridView1.Rows[index-1].Selected = true;
}
}
}
And here the screen after swapping two rows.
Or is there a better way to do it?
Okay, I found a solution myself. It was enough to set dataGridView1.CurrentCell. Maybe it will be useful to someone. The solution is here:
private void moveUp()
{
if (dataGridView1.RowCount > 0)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int rowCount = dataGridView1.Rows.Count;
int index = dataGridView1.SelectedCells[0].OwningRow.Index;
if (index == 0)
{
return;
}
var temp = listApp[index];
var temp2 = listApp[index - 1];
listApp.Remove(temp);
listApp.Remove(temp2);
listApp.Insert(index-1, temp);
listApp.Insert(index, temp2);
dataGridView1.DataSource = null;
dataGridView1.DataSource = listApp;
dataGridView1.SelectedCells[0].Selected = false;
dataGridView1.Rows[index-1].Selected = true;
dataGridView1.CurrentCell = dataGridView1[1, index - 1];
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.