gpt4 book ai didi

c# - 在 DataGridView 中以编程方式选择行

转载 作者:IT王子 更新时间:2023-10-29 04:33:04 28 4
gpt4 key购买 nike

我想在某些事件发生后选择以前选择的行,我的代码如下。

int currentRow = dgvIcbSubsInfo.CurrentCell.RowIndex;
//code to execute
dgvIcbSubsInfo.Rows[currentRow].Selected = true;

执行代码后预览如下。但我需要在 id = 1272741(蓝色选择)中获取符号 > 而不是在 1272737

enter image description here

最佳答案

您可能已经看过 DataGridView.CurrentRow Property ,这是一个只读属性:

Gets the row containing the current cell.

但是在备注部分,写着:

To change the current row, you must set the CurrentCell property to a cell in the desired row.

此外,来自 DataGridView.CurrentCell Property ,我们发现:

When you change the value of this property, the SelectionChanged event occurs before the CurrentCellChanged event. Any SelectionChanged event handler accessing the CurrentCell property at this time will get its previous value.

因此,您无需实际选择 currentRow,因为它会在您设置 CurrentCell 值时被选中(除非您有一些代码要在内部执行SelectionChangedCurrentCellChanged 事件之间的当前范围)。试试这个:

//dgvIcbSubsInfo.Rows[currentRow].Selected = true;
dgvIcbSubsInfo.CurrentCell = dgvIcbSubsInfo.Rows[currentRow].Cells[0];

关于c# - 在 DataGridView 中以编程方式选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14576803/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com