.net DataGridView:如何聚焦整行而不是单个单元格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/285829/
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
DataGridView: how to focus the whole row instead of a single cell?
提问by Tomas Sedovic
I'd like to use the DataGridView control as a list with columns. Sort of like ListView in Details mode but I want to keep the DataGridView flexibility.
我想将 DataGridView 控件用作带有列的列表。有点像详细模式下的 ListView,但我想保持 DataGridView 的灵活性。
ListView(with Detailsview and FullRowSelectenabled) highlights the whole line and shows the focus mark around the whole line:
ListView(启用详细信息视图和FullRowSelect)突出显示整行并显示整行周围的焦点标记:
DataGridView(with SelectionMode= FullRowSelect) displays focus mark only around a single cell:
DataGridView(带有SelectionMode= FullRowSelect)仅在单个单元格周围显示焦点标记:
So, does anyone know of some (ideally) easy way to make the DataGridView row selection look like the ListView one?
I'm not looking for a changed behaviour of the control - I only want it to look the same.
Ideally, without messing up with the methods that do the actual painting.
那么,有没有人知道一些(理想情况下)简单的方法来使 DataGridView 行选择看起来像 ListView 行选择?
我不是在寻找控件的改变行为 - 我只希望它看起来一样。
理想情况下,不要弄乱进行实际绘画的方法。
回答by Tomas Sedovic
Put this code either into your form's constructor or set it in datagridview's Propertiesusing the IDE.
将此代码放入表单的构造函数中,或使用 IDE在 datagridview 的属性中设置它。
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dgv.MultiSelect = false;
dgv.RowPrePaint +=new DataGridViewRowPrePaintEventHandler(dgv_RowPrePaint);
Then paste the following event into the form code:
然后将以下事件粘贴到表单代码中:
private void dgv_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
{
e.PaintParts &= ~DataGridViewPaintParts.Focus;
}
And it works! :-)
它有效!:-)
"dgv" is the DataGridViewin question and "form" is the Formthat contains it.
“dgv”是有问题的DataGridView,“form”是包含它的Form。
Note, that this soulution doesn't display the dotted rectangle around the whole row. Instead, it removes the focus dots entirely.
请注意,此解决方案不会显示整行周围的虚线矩形。相反,它完全删除了焦点点。
回答by L.E.
How about
怎么样
SelectionMode == FullRowSelect
and
和
ReadOnly == true
It works for me.
这个对我有用。

