【发布时间】:2012-03-16 18:17:42
【问题描述】:
如何在c#中通过点击按钮改变datagridview的当前行?
【问题讨论】:
标签: c# winforms datagridview indexing
如何在c#中通过点击按钮改变datagridview的当前行?
【问题讨论】:
标签: c# winforms datagridview indexing
如果您的意思是更改选定的行索引,这应该可以:
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 尤里
此外,如果您希望移动选择箭头并且您的行不可见,则:
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
【讨论】: