且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

DataGridView:如何集中整行而不是单个单元格?

更新时间:2023-12-03 11:26:04

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.