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

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

问题描述

我想将 DataGridView 控件用作带有列的列表.有点像详细模式下的 ListView,但我想保持 DataGridView 的灵活性.

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(启用Details 视图和FullRowSelect)突出显示整行并显示整行周围的焦点标记:

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

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

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

那么,有没有人知道一些(理想的)简单方法可以使 DataGridView 行选择看起来像 ListView 行选择?
我不是在寻找改变的控件行为 - 我只希望它看起来一样.
理想情况下,不要弄乱进行实际绘画的方法.

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.

推荐答案

将此代码放入表单的构造函数中,或使用 IDE 将其设置在 datagridview 的属性中.

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;
}

它有效!:-)

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

"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天全站免登陆