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

发布于 2024-07-11 12:52:52 字数 143 浏览 7 评论 0原文

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

我正在使用 Windows 窗体

How to change the row position of virtual mode DataGridView?

I am using Windows Forms.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

少跟Wǒ拽 2024-07-18 12:52:53
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
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
辞慾 2024-07-18 12:52:52

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

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

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

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.

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

苏佲洛 2024-07-18 12:52:52

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

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

我相信这会滚动网格。 另外,为了绝对安全,您可能需要在另一行代码之前添加此行...

dgv.CurrentCell = null;

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

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

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.

水波映月 2024-07-18 12:52:52

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

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

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

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

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:

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.

对不⑦ 2024-07-18 12:52:52
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文