如何更改虚拟模式DataGridView的行位置?

我正在使用 Windows窗体

有帮助吗?

解决方案

Marcus的回答是正确的,但您可能还需要设置DataGridView的当前单元格属性...

dgv.CurrentCell = dgv.Rows[0].Cells[0];

我相信这会滚动网格。另外,为了绝对安全,您可能希望在其他代码行之前添加它...

dgv.CurrentCell = null;

这将确保如果您想要的行已经是活动行但只是滚动到视图外,它会将其滚动回视图。

其他提示

您必须清除旧位置并设置新位置

集合dataGridView1.SelectedRows具有当前选定的行。根据网格的MultiSelect属性,您可能需要循环遍历SelectedRows中的所有行,并将它们标记为未选中。如果您是单选模式,只需将新行设置为选中,即可清除旧选择。

要选择特定行(在本例中为索引0处的行),只需添加该行即可    dataGridView1.Rows [0] .Selected = true;

您似乎不仅需要设置所选行,还需要设置显示的行。您可以使用DataGridView上的 FirstDisplayedScrollingRowIndex 属性访问后者。其中一个有用的设置:

int lastShown = FirstDisplayedScrollingRowIndex + DisplayedRowCount(false) - 2;

if (lastShown < yourIndex)
  FirstDisplayedScrollingRowIndex += yourIndex - lastShown;
else if (FirstDisplayedScrollingRowIndex > yourIndex)
  FirstDisplayedScrollingRowIndex = yourIndex;
当以编程方式向上/向下滚动时,

将确保新选择的行不会从屏幕上消失。

Else
        If i = rowcount Then
            Me.GridSaleItem.CurrentCell = Me.GridSaleItem.Rows(i - 1).Cells(0)
            Me.GridSaleItem.Rows(i - 1).Selected = True
        End If
    End If
Next
Private Sub GridSaleItem_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridSaleItem.SelectionChanged
    Dim rowcount As Integer
    rowcount = GridSaleItem.Rows.Count
    For i As Integer = 1 To rowcount
        If i = 1 Then
            '
        Else
            If i = rowcount Then
                Me.GridSaleItem.CurrentCell = Me.GridSaleItem.Rows(i - 1).Cells(0)
                Me.GridSaleItem.Rows(i - 1).Selected = True
            End If
        End If
    Next

End Sub
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top