Changing the datagridview index

How to change current datagridview row in C # by clicking a button?

+4
source share
2 answers

+1 Yuri

Also, if you want to move the selection arrow and your line is not visible, follow these steps:

grid.FirstDisplayedScrollingRowIndex = grid.Rows[2].Index; DataGgridridView1.Refresh() grid.CurrentCell = grid.Rows[2].Cells(1) // need to ensure that this is an existing, visible cell grid.Rows[2].Selected = True 
+2
source

If you mean changing the selected row index, this should work:

 private void button_Click(object sender, EventArgs e) { grid.ClearSelection(); // Select the third row. grid.Rows[2].Selected = true; } 

If you want to swap lines (for example, exchange data in the 1st and 3rd lines), select the option:

 int currentRowIndex = 0; int newRowIndex = 2; var currentRow = grid.Rows[currentRowIndex]; var rowToReplace = grid.Rows[newRowIndex]; grid.Rows.Remove(currentRow); grid.Rows.Remove(rowToReplace); grid.Rows.Insert(currentRowIndex, rowToReplace); grid.Rows.Insert(newRowIndex, currentRow); 
+2
source

Source: https://habr.com/ru/post/1398566/


All Articles