数据网格视图...以编程方式设置选择行索引不会将 CurrentRow.Index 设置为相同? [英] Data Grid View...programmatically setting the select row index doesn't set the CurrentRow.Index to the same?

查看:25
本文介绍了数据网格视图...以编程方式设置选择行索引不会将 CurrentRow.Index 设置为相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码

CurrentSelectedRow = Me.dgvPreviouslyCut.CurrentRow.Index

将用户单击的当前选定行存储在数据网格视图控件中.刷新数据网格视图的数据源后,这段代码

stores the current selected row that has been clicked by the user in a data grid view control . After refreshing the datasource for the data grid view, this code

Me.dgvPreviouslyCut.Rows(CurrentSelectedRow).Selected = True

以编程方式重新选择同一行.

programmatically re-selects the same row.

尽管如此

 Me.dgvPreviouslyCut.CurrentRow.Index

始终设置为零,而不是您期望的变量 CurrentSelectedRow.

is always set to zero, NOT the variable CurrentSelectedRow as you would expect.

为什么以编程方式设置选择行索引不会导致属性 CurrentRow.Index 设置为相同?

Why does programmatically setting the select row index not cause the property CurrentRow.Index to be set to the same?

推荐答案

CurrentRow 是包含当前活动单元格的行.当您将 DataGridView 绑定到外部数据源时,此属性将重置为其默认值,即第一列中的第一个单元格.

CurrentRow is the row containing the currently active cell. When you bind the DataGridView to an external data source, this property is reset to its default value which is the first cell in the first column.

SelectedRow 是当前选中/突出显示的行.它可能是一行或多行,具体取决于 MultiSelect 属性.要选择一行,您必须将其 Selected 属性设置为 true.

SelectedRow is the row which is currently selected/highlighted. It may be one or more rows depending on MultiSelect property. To select a row you have to set its Selected property to true.

通过将行设置为选中,您只是将其突出显示而不是使其处于活动状态.

By setting the row as selected you merely keeping it highlighted not making it active.

要保留当前单元格,您必须存储当前单元格的行和列索引.要获得它们,请使用 CurrentCellAddress 属性.刷新后,DataSource 使用这些索引设置 CurrentCell 属性.

To retain the current cell you have to store the Current cell's row and column index. To get them use the CurrentCellAddress property. Once your refresh the DataSource set the CurrentCell property using these indexes.

dataGridView1.CurrentCell = dataGridView1.Rows(rowindex).Cells(columnindex);

这篇关于数据网格视图...以编程方式设置选择行索引不会将 CurrentRow.Index 设置为相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆