Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

.net - DataGridView: how to focus the whole row instead of a single cell?

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:
selected row in ListView control

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...