datagridview索引更改

本文关键字:索引 datagridview | 更新日期: 2023-09-27 18:25:50

如何通过单击按钮更改c#中数据网格视图的当前行?

datagridview索引更改

如果您的意思是更改所选的行索引,这应该有效:

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

如果你想交换行(例如,交换第一行和第三行的数据),这里有一个选项:

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);

+1 Yuriy

此外,如果你想移动选择箭头,而你的行不可见,那么:

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