C# 在 DataGridView 中选择一行并在行标题上显示箭头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1135304/
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 a row in a DataGridView and having the arrow on the row header follow
提问by BrianK
This is in C#. If I select a row in a DataGridView with DataGridViewRow.Selected = true, the row selects just fine, but the arrow in the "column header" (the grey very leftmost column) doesn't follow. How do I set that?
这是在 C# 中。如果我使用 DataGridViewRow.Selected = true 在 DataGridView 中选择一行,该行选择得很好,但“列标题”(最左边的灰色列)中的箭头不会跟随。我该如何设置?
Another question: If I set a column format to "centered" in the designer, the column header is still left aligned. How do I set the column header to be centered too?
另一个问题:如果我在设计器中将列格式设置为“居中”,列标题仍然左对齐。如何将列标题也设置为居中?
Thanks, Brian
谢谢,布赖恩
采纳答案by Kredns
This is straight from google:
这是直接来自谷歌:
In a DataGridView, the selected row and the current row (indicated by an arrow in the row header) may not be the same row. In addition, we could select multiple rows in a DataGridView but the current row can only be one row. When the SelectionMode property of the DataGridView is set to FullRowSelect, the current row will be always selected. If you'd like to change the current row in a DataGridView control, you may set the CurrentCell property
在 DataGridView 中,所选行和当前行(由行标题中的箭头指示)可能不是同一行。此外,我们可以在 DataGridView 中选择多行,但当前行只能是一行。当 DataGridView 的 SelectionMode 属性设置为 FullRowSelect 时,将始终选择当前行。如果您想更改 DataGridView 控件中的当前行,您可以设置 CurrentCell 属性
dataGridView1.CurrentCell = dataGridView1.Rows[1].Cells[0];
If you'd like to just change the selected row, you may set the Selected property of the row you want to true.
如果您只想更改选定的行,您可以将您想要的行的 Selected 属性设置为 true。
dataGridView1.CurrentRow.Selected = false;
dataGridView1.Rows[1].Selected = true;
回答by Kelsey
To answer the second part of you question, make sure your setting the header stylefor the column to centered as well.
要回答问题的第二部分,请确保将列的标题样式也设置为居中。