2

If you have a Datagridview with 2 columns, one is a TextBoxColumn and the other is a ButtonColumn:

Select the Text cell. Now highlight/select the text inside the cell and while selecting (holding mousebutton down) and if you move the mousecursor over the neighboring ButtonColumn and release the mousebutton, then the DatagridView fires CellContentClick for the button column even though I did MouseDown inside the textcell.

I would expect that for CellContentClick to be triggered, that MouseDown and MouseUp must both have been performed in the same Cell. My users also expect that.

Does anybody know a workaround/fix for this?

Dorf
  • 71
  • 9
  • Did you debug the code and check what value you get in `e.ColumnIndex` ? is it the same as TextBoxColumn or ButtonColumn? – Chetan May 20 '18 at 08:19
  • 1
    I can confirm this. Weird, borderline to bug. But imo any use of CellContentClick is error prone and best avoided altogether. – TaW May 20 '18 at 08:53
  • Thanks for the confirmation. Microsoft recommends using CellContentClick to detect a button push. Do you have another method to detect a button push? – Dorf May 20 '18 at 11:20
  • @Chetan yes I checked and ColumnIndex is set to the column of the Button – Dorf May 20 '18 at 11:22
  • Well it is one way, but CellClick should work just as well. – TaW May 20 '18 at 12:14
  • Nice, problem not there with CellClick event. Put as answer and I will give you the points – Dorf May 20 '18 at 12:57

1 Answers1

0

I usually avoid using CellContentClick and go for CellClick.

This not only avoids this weird behaviour but also other pitfalls of CellContentClick: User must actually hit the text to make it work, not the empty space around it.

For some special cases this may even be fine, like when a cell is really large and one wants to discern clicking it for selection from clicking the content for editing.

But CellClick will work fine for normal as well as for button cells and others.

Yes MSDN talks about usings it for DataGridViewButtonColumns but it is just a possible use and by no means reuqired.

TaW
  • 53,122
  • 8
  • 69
  • 111