datagridview索引更改

时间:2012-02-27 08:51:06

标签: c# winforms datagridview indexing

如何通过单击按钮更改c#中datagridview的当前行?

2 个答案:

答案 0 :(得分:2)

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

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

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

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 :(得分:2)

+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