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

时间:2022-09-28 17:27:57

How to change the row position of virtual mode DataGridView?

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

I am using Windows Forms.

我正在使用Windows窗体。

5 个解决方案

#1


Marcus's answer is correct, but you may also need to set the DataGridView's current cell property...

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

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

I believe this will scroll the grid. Also, to be absolutely safe, you may want to add this before the other line of code...

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

dgv.CurrentCell = null;

This will ensure that if the row you want is already the active row but just scrolled out of view, it will scroll it back into view.

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

#2


You have to clear the old position and set a new one

你必须清除旧位置并设置一个新位置

The collection dataGridView1.SelectedRows has the current selected Rows. Depending on the MultiSelect property of the grid you may have to loop through all the rows in the SelectedRows and mark them as unselected. If you are single selection mode, just setting the new row as selected should clear the old selection.

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

To select a particular row (in this case the one at index 0) you just add the line dataGridView1.Rows[0].Selected = true;

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

#3


You seem to require not only setting the selected row, but also the displayed row. You can access the latter with the FirstDisplayedScrollingRowIndex property on your DataGridView. One of the useful setups:

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

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

if (lastShown < yourIndex)
  FirstDisplayedScrollingRowIndex += yourIndex - lastShown;
else if (FirstDisplayedScrollingRowIndex > yourIndex)
  FirstDisplayedScrollingRowIndex = yourIndex;

will make sure your newly selected row does not disappear off the screen when scrolling up/down programmatically.

当以编程方式向上/向下滚动时,将确保新选择的行不会从屏幕上消失。

#4


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

#5


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

#1


Marcus's answer is correct, but you may also need to set the DataGridView's current cell property...

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

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

I believe this will scroll the grid. Also, to be absolutely safe, you may want to add this before the other line of code...

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

dgv.CurrentCell = null;

This will ensure that if the row you want is already the active row but just scrolled out of view, it will scroll it back into view.

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

#2


You have to clear the old position and set a new one

你必须清除旧位置并设置一个新位置

The collection dataGridView1.SelectedRows has the current selected Rows. Depending on the MultiSelect property of the grid you may have to loop through all the rows in the SelectedRows and mark them as unselected. If you are single selection mode, just setting the new row as selected should clear the old selection.

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

To select a particular row (in this case the one at index 0) you just add the line dataGridView1.Rows[0].Selected = true;

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

#3


You seem to require not only setting the selected row, but also the displayed row. You can access the latter with the FirstDisplayedScrollingRowIndex property on your DataGridView. One of the useful setups:

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

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

if (lastShown < yourIndex)
  FirstDisplayedScrollingRowIndex += yourIndex - lastShown;
else if (FirstDisplayedScrollingRowIndex > yourIndex)
  FirstDisplayedScrollingRowIndex = yourIndex;

will make sure your newly selected row does not disappear off the screen when scrolling up/down programmatically.

当以编程方式向上/向下滚动时,将确保新选择的行不会从屏幕上消失。

#4


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

#5


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