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

6 comments:

Anonymous 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!

Unknown 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.

Anonymous 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

eddyshaw9272711 said...

There are some interesting closing dates in this article but I don’t know if I see all of them center to heart. There's some validity however I'll take hold opinion until I look into it further. Good article , thanks and we wish more! Added to FeedBurner as properly best online casino