C# 在 DataGridView 中以编程方式选择行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14576803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selecting rows programmatically in DataGridView
提问by manoj
I want to select row of previously selected rows after some event my code is as below.
我想在某些事件之后选择先前选定的行,我的代码如下。
int currentRow = dgvIcbSubsInfo.CurrentCell.RowIndex;
//code to execute
dgvIcbSubsInfo.Rows[currentRow].Selected = true;
after executing the code the preview will be as below. but i need to get the symbol >
in id = 1272741 (blue selection) and not in 1272737
执行代码后,预览将如下所示。但我需要>
在 id = 1272741(蓝色选择)而不是 1272737 中获取符号
采纳答案by Alex Filipovici
Probably you might have taken a look at the DataGridView.CurrentRow Property, which is a read-only property:
您可能已经看过DataGridView.CurrentRow 属性,它是一个只读属性:
Gets the row containing the current cell.
获取包含当前单元格的行。
But in the remarks section, there is written:
但在备注部分,有写:
To change the current row, you must set the
CurrentCell
property to a cell in the desired row.
要更改当前行,您必须将该
CurrentCell
属性设置为所需行中的单元格。
Also, from the DataGridView.CurrentCell Property, we find out that:
此外,从DataGridView.CurrentCell 属性,我们发现:
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.
当您更改此属性的值时,SelectionChanged 事件发生在 CurrentCellChanged 事件之前。此时任何访问 CurrentCell 属性的 SelectionChanged 事件处理程序都将获得其先前的值。
So, there is no need that you actually select the currentRow
becasue it will be selected when you set the CurrentCell
value (unless you have some code to be executed inside the current scope between the SelectionChanged
and CurrentCellChanged
events). Try this:
因此,您无需实际选择,currentRow
因为在设置CurrentCell
值时将选择它(除非您在SelectionChanged
和CurrentCellChanged
事件之间的当前范围内有一些要执行的代码)。尝试这个:
//dgvIcbSubsInfo.Rows[currentRow].Selected = true;
dgvIcbSubsInfo.CurrentCell = dgvIcbSubsInfo.Rows[currentRow].Cells[0];
回答by mihirj
I think you wish to highlight the row. Please try following code, I think it might help:
我认为您希望突出显示该行。请尝试以下代码,我认为它可能会有所帮助:
Color color = dgv.Rows[prevRowIndex].DefaultCellStyle.SelectionBackColor;
dgv.Rows[curRowIndex].DefaultCellStyle.SelectionBackColor = color;
回答by Goal Man
Try the following to change the current row. Since the OP is a little unclear as to what row should be the new row, my example simply shows moving from the current row to the previous row (if there is a previous row). The first line of code is optional. You can also hardcode col to 0 (or some other column) to use a fixed column if you don't want to use FullRowSelect.
尝试以下操作来更改当前行。由于 OP 不太清楚哪一行应该是新行,我的示例只是显示从当前行移动到前一行(如果有前一行)。第一行代码是可选的。如果您不想使用 FullRowSelect,您还可以将 col 硬编码为 0(或某些其他列)以使用固定列。
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
int row = dataGridView.CurrentCell.RowIndex;
int firstRow = dataGridView.Rows.GetFirstRow(DataGridViewElementStates.None);
if (row != firstRow)
{
row--;
int col = dataGridView.CurrentCell.ColumnIndex;
dataGridView.CurrentCell = dataGridView[col, row];
}