【问题标题】:datagridview index changedatagridview 索引更改
【发布时间】:2012-03-16 18:17:42
【问题描述】:

如何在c#中通过点击按钮改变datagridview的当前行?

【问题讨论】:

    标签: c# winforms datagridview indexing


    【解决方案1】:

    如果您的意思是更改选定的行索引,这应该可以:

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

    【讨论】:

    • 我想更改当前行。例如当前行位置是 5 我想把它改成 8。
    • @user1235144:你什么意思?您的意思是将行及其数据移动到不同的位置或只是更改选择?
    • 只是把黑色的小箭头改成另一条记录。
    • @user1235144:我添加了一个 sn-p 用于交换行到我的答案。请查看是否是您要查找的内容。
    • 我认为你没有理解我的问题。我有一个名为 NEXT 的按钮。我有一个网格视图。当用户单击 NEXT 时,我希望 sn-p 转到下一条记录。我想跨越记录。 ——
    【解决方案2】:

    +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
    

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多