DataGridView:如何集中整行而不是单个单元格? [英] DataGridView: how to focus the whole row instead of a single cell?

查看:88
本文介绍了DataGridView:如何集中整行而不是单个单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用DataGridView控件作为列的列表。排序像ListView在细节模式,但我想保持DataGridView的灵活性。



ListView (使用详细信息视图和 FullRowSelect 启用)突出显示整行,并显示整个行的焦点标记:



DataGridView (使用 SelectionMode = FullRowSelect )仅在单个单元格周围显示焦点标记:



所以,有没有人知道一些(理想的)简单的方法,使DataGridView行选择看起来像ListView? br>
我不是在寻找一个改变的控件的行为 - 我只希望它看起来一样。

理想情况下,不要弄乱实际绘画的方法。 / p>
将这段代码放在你的窗体的构造函数中,或者使用IDE在datagridview的属性中设置。

  dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect; 
dgv.MultiSelect = false;
dgv.RowPrePaint + = new DataGridViewRowPrePaintEventHandler(dgv_RowPrePaint);

然后将以下事件粘贴到表单代码中:

  private void dgv_RowPrePaint(object sender,DataGridViewRowPrePaintEventArgs e)
{
e.PaintParts& =〜DataGridViewPaintParts.Focus;
}

它的工作原理! :-)



dgv是有问题的 DataGridView ,form是包含它的 Form



请注意,这个灵魂没有显示整行的虚线矩形。而是完全删除焦点。


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.

ListView (with Details view and FullRowSelect enabled) highlights the whole line and shows the focus mark around the whole line:

DataGridView (with SelectionMode = FullRowSelect) displays focus mark only around a single cell:

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.

解决方案

Put this code either into your form's constructor or set it in datagridview's Properties using the IDE.

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 DataGridView in question and "form" is the Form that contains it.

Note, that this soulution doesn't display the dotted rectangle around the whole row. Instead, it removes the focus dots entirely.

这篇关于DataGridView:如何集中整行而不是单个单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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