Thursday, January 31, 2008

Hide selection in DataGridView

Today I was faced at work with the problem that when there is a datagridview always the first cell/row/column depending on the selection mode is selected. I looked therefore for a property or method to have nothing selected. The data grid view provides a method called “clear selection” which can be called with

dataGridView1.ClearSelection();

but for some reason this did not work for me. I tried to create a new project and to do the same thing and suddenly the “clear selection” method worked perfectly. To resolve my problem I finally used the following code:

foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
cell.Selected = false;

This simply takes the list of selected cells and sets the "Selected"-property to false. This can also be done with only the rows or columns with the following code snippets:

foreach (DataGridViewRow row in dataGridView1.SelectedRows)
row.Selected = false;

foreach (DataGridViewColumn column in dataGridView1.SelectedColumns)
column.Selected = false;

If you use the version for individual cells you cover also the row and column selection. So it is not needed to have all three loops.
I tried also with the code
if(dataGridView1.SelectedCells.Count > 0)
dataGridView1.SelectedCells[0].Selected = false;

Which surprisingly did produce an error in the project but worked when I tried it in the new "clean" project. Please note that this code can only be used if multiple selection is not allowed!
I could not find out what where the differences between the empty project and my other project but if you encounter a similar behaviour the foreach loop should solve the issue; at least id did for me :-)


Tested with:
Visual Studio 2005 Express Edition
.NET Framework 2.0

8 comments:

Theo de Roo said...

Thank you Manfred, this solved me a headache. Had tried to change the selection colors in order to hide the selection but that did not work. This is a much neater solution, now automatically run this in the SelectionChanged event so that none of the cells ever show up as selected.

Thanks!

Manfred said...

I'm pleased to hear that; I am always happy if I can help someone with my posts :-)

Anonymous said...

Thanks for the info. I've finally figured it out (with your help). It's an issue with timing. I was selecting a cell using the MouseDown event, which fires before the cell is selected. Since that is the case the ClearSelection (and all other efforts to clear) cleared nothing and then the cell got selected. Once I moved the code to MouseUp (after a cell is selected) the ClearSelection worked for me.

codeprojects.net said...

thank for soln I was looking of this since long
Find more on datagridview
http://expertpoint.blogspot.com

Ravi said...

I tried dataGridView1.ClearSelection() in Form1_Load event and then it works from me

Leslie Lim said...


amazing and nice looking site please love it and make more effective… keep it up.

Bubble
www.gofastek.com

joy said...

I am very happy to visit your site and i did like it. I was able to get some informative topics. keep it up and you can also visit my site for your reference. Thank you and have a good day.

n8fan.net

www.n8fan.net

sarah lee said...

I really enjoyed reading your article. I found this as an informative and interesting post, so i think it is very useful and knowledgeable. I would like to thank you for the effort you have made in writing this article.


edupdf.org