Take the 2-minute tour ×

The DataGridView control allows you to click the row headers and select the rows (the whole row is highlighted), or use the mouse and click and drag to select multiple rows, etc. to select all of them.

Anyway, I need the rows to stay selected if the user decides to click on a cell somewhere in the DataGridView. So, if the user clicks on a cell in one of the selected rows, the selected rows should stay highlighted. If the user clicks a cell in a different, unselected row, all the currently selected cells should stay highlighted. As a matter of fact, I don't want the row selection to change at all unless they use the row headers.

Is this something that's easy to configure, or am I in for a few days of hacking?

share|improve this question
Ok, well, I thought of different way to achieve what I wanted. You guys don't have to bother answering this one anymore! Thanks to everyone. –  Isaac Bolinger Feb 8 '11 at 6:00
Best practice is to post your own solution and accept it - you may be helping someone in the future. –  Kirk Broadhurst Feb 8 '11 at 6:44
Meanwhile you'd be better off simply training your users that when they click a cell they are selecting a row. They should learn that quickly - it's default behaviour. –  Kirk Broadhurst Feb 8 '11 at 6:45
No it isn't. You have to have it on FullRowSelect for that to be true. The little triangle on the rowheader doesn't mean the row is in the collection of selected rows. By default I think its set to RowHeaderSelect. Anyway, the default behavior of the datagridview is utter BS, I've really changed it alot –  Isaac Bolinger Feb 8 '11 at 9:21
Anyway, I haven't solved this problem... I only changed my design so I didn't have to solve it :( –  Isaac Bolinger Feb 8 '11 at 9:22

2 Answers 2

up vote 2 down vote accepted

Not easy to configure. Spend a few days with the DataGridView control, and you'll notice that nothing about it is easy to configure, unless the developers happened to decide to expose it as a property. And only the simplest of things are exposed this way, so don't waste too much time looking.

Of course, it is possible. I've spent way too much of my life subclassing the DataGridView control and overriding built-in behavior that strikes me as stupid. But I really don't recommend it to anyone.

More to the point, you should also seriously reconsider whether you even want to make this particular change. I would have no idea what was going on if software started to do that. I'd immediately suspect that my shift key or mouse button were stuck and try banging them unstuck. The next step would be restarting my computer. Overall, not a very positive user experience. This isn't the way that the control behaves for a reason. Do consider very carefully what it actually means semantically for a cell to be highlighted—generally, the implication is that you can change the value of or delete all selected cells at once. If that's not how your implementation is going to work, you probably shouldn't do it in the first place.

share|improve this answer
It's too late. I've subclassed the datagridview too and I've already fixed a great many annoying behaviors of it and added some new ones that were useful to me. I've figured out how to add my own controls in there... I can't get away from the damn DGV now. And the more deeply I delve into its workings the harder it seems to be to find help about it. Ah well. –  Isaac Bolinger Feb 8 '11 at 5:16
@IsaacB: Yeah, that's because the DGV is kind of a bastard control that no one wants to use. Everyone doing anything non-trivial ends up reimplementing the damn thing, but certainly doesn't want to spend their time helping anyone else do the same. Painful memories, I suppose. If you want to go through with this, you have no choice but to take all of the selection behavior into your own hands. Prevent the control from adding cells, rows, or columns to the selected items properties, and add them yourself in an override of the OnRowHeaderMouseClick method. –  Cody Gray Feb 8 '11 at 5:20
"I've spent way too much of my life subclassing the DataGridView control" I hear ya! –  Sahuagin Jun 30 '12 at 22:45

I would say its not a good idea to to this and its unwanted behavior from users perspective you will need to teach them that your rows get selected only by row headers not by clicking on them (believe me they will be frustrated). In a way you are refraining the user from selecting the rows at all.

That said, if you are keen to go on this design then you will need to do it yourself. GridView doesn't have any built behaviour for this. You can fiddle with RowChanged events and CanSelect properties & do some overriding.

And then there's a RowHeaderMouseClick event you can utilize for Row Selection from Row Header.

share|improve this answer
No, its not too much of an issue. My user base is 4-5 people at most. They're already used to clicking the row headers for deleting multiple rows anyway. The fact that the row is selected by clicking on a cell has no meaningful use in my program right now. I'm not giving anything up by getting rid of it. –  Isaac Bolinger Feb 8 '11 at 5:08
@IsaacB: Ah, do you have the DataGridViewSelectionMode property set to "FullRowSelect"? That would cause clicking on a single cell to select the entire row. If that's not what you want, change that property to something like "RowHeaderSelect". (All the possible values are listed here.) –  Cody Gray Feb 8 '11 at 5:13
@IsaacB well then i have mentioned about how to that by utilizing RowHeaderMouseClick event..good luck :) –  Shekhar_Pro Feb 8 '11 at 5:13
@Cody Gray RowHeaderSelect mode still selects the cell if clicked on the cells.. OP still need to override this behavior –  Shekhar_Pro Feb 8 '11 at 5:15
@Shekhar: No, it doesn't. Clicking one of the cells selects that cell, but it does not select the entire row. It's not clear if that will actually solve the problem discussed in the question, but it might be an answer to his last comment. –  Cody Gray Feb 8 '11 at 5:17

Your Answer


