如何更改虚拟模式DataGridView的行位置?
如何更改虚拟模式DataGridView的行位置?
我正在使用 Windows 窗体。
How to change the row position of virtual mode DataGridView?
I am using Windows Forms.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须清除旧位置并设置一个新位置
集合 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;
马库斯的答案是正确的,但您可能还需要设置 DataGridView 的当前单元格属性...
我相信这会滚动网格。 另外,为了绝对安全,您可能需要在另一行代码之前添加此行...
这将确保如果您想要的行已经是活动行但只是滚动到视图之外,它将滚动回视图。
Marcus's answer is correct, but you may also need to set the DataGridView's current cell property...
I believe this will scroll the grid. Also, to be absolutely safe, you may want to add this before the other line of code...
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.
您似乎不仅需要设置所选行,还需要设置显示的行。 您可以使用 DataGridView 上的
FirstDisplayedScrollingRowIndex
属性访问后者。 有用的设置之一:将确保以编程方式向上/向下滚动时,新选择的行不会从屏幕上消失。
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:will make sure your newly selected row does not disappear off the screen when scrolling up/down programmatically.