DataGridView的:如何围绕一个单细胞的整排的呢?单细胞、DataGridView

2023-09-02 10:55:26 作者:花落浅殇

我想用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 (用的详细信息的看法和 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.

推荐答案

将这个code要么到窗体的构造函数或将其设置在DataGridView中的的属性的使用IDE。

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

那么下面的事件粘贴到表$ ​​C $ C:

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是的的DataGridView 的问题与形是的表格的包含它。

"dgv" is the DataGridView in question and "form" is the Form that contains it.

请注意,这soulution不显示整个行周围的虚线框。相反,它消除了聚焦点完全。

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