SelectedRows.count value inconsistent

tcl4p

Well-known member
Joined
Feb 29, 2008
Messages
48
Programming Experience
1-3
I have a form with a DataGridView control. I notice that if I click on a cell on a row and then check, the SelectedRows.count will equal 0 even though the click event will trigger. If I click on the row selector button on the left side of the row the SelectedRows.Count is correct at 1. Is there any way to get the SelectedRows.Count or another property that will return a consistent value that will indicate the user has selected a row when they click anywhere on the row.
It's a bit confusing because when you click on a cell the entire row is highlighted and I know there's no way the user is always going to click on that row selection button. As a side question is there a property to delete that row selection button?

Thanks,
Tom
 
Perhaps it's because of the event you are using (which is not clear which), DGV control has many events and the order of which things happens may not always be too obvious. I checked the SelectedRows.Count from SelectionChanged event for example and could not see the problem you are describing happen there.
 
Thanks, but problems

John, thanks for the reply. I'm currently using the "Click" event to capture the data from the grid row prior to the user clicking a button to execute an action. I tried using the "SelectionChanged" event, but ran into a problem in as much as I'm loading the grid based on a selection made from another control and the "SelectionChanged" event is kicked off when the grid is loaded. Since I'm using the Row Count to insure a value was selected i.e. before doing a delete of a row, but return a error message when the selected row count is 0, I would get the error message during the load of the grid. Either there's an event I'm missing that will capture/change the selected row count when the user clicks on either the row selection button or on any one of the cells or I'll try and use both the click event and the "cellcontentclick" event together. That's my next step.

Regards,
Tom
 
Can't you validate the selection in button Click, prior to performing the action the requires a selection?
 
John,
Thanks again for the reply. I may be and probably am missing something, but I believe I'am actually doing what you selected. On the button click I was trying to use the SelectedRows.count to make sure the user had selected a row. I was using the click event to get the ID for that row and unload other data before processing it on the button click. I did find one other interesting fact, well maybe no so interesting or helpful, but if you use the click event and also use the Cellcontentclick event, the click event blocks the CellContentClick. That is to say if I click on the row, either on the row button or in one of the row cells, the click event will fire, but not the cellcontentclick event. If you remove the click event the cellcontentclick event fires.
All that said, I'm still in the same position of trying to find the best way to validate the fact that a user has indeed selected a row. I'm guessing instead of looking at the SelectedRows.count, I'll just look to make sure the ID variable for the row is valued (>0).

Thanks,
Tom
 
I really don't understand why you talk about all these other events, do they have a purpose? From my understanding you have a button that you click to perform some action on selection, that action depends on there being a selection. If this is it, then all you have to do is check if Selection.Count>0 in button click handler before doing that action.
 
John,
Again I'm probably missing something but the datagridview does not have a selection.count property.
 
What if the "selection" means SelectedRows? Isn't that what we're discussing here? Didn't you say you need at least one row to be selected to perform some action on it?
 
John,
Again thanks for your time and more importantly your patience. I actually got things running using the click event and seeing if the ID value for that row is greater then zero. The click event is consistent when the user clicks either on the row button or clicks in any one of the cells in that row. As I mentioned before I'm probably missing something, but it seems to me there should be one consistent event-property that is consistent when the user clicks either on the row button or on one of the cells in that row that will tell you that, in fact, the row has been clicked, which can be checked from another control or form event. All that said, I'm sure I'll be back with more questions for you in the future and can only ask for the same patience and understanding.

Regards,
Tom
 
Back
Top