DataGridView - how can I make a checkbox act as a radio button?

前端 未结 4 1340
后悔当初
后悔当初 2021-01-21 00:18

I have a Windows Forms app that displays a list of objects in a DataGridView.

This control renders bool values as checkboxes.

There\'s a set of three checkboxe

4条回答
  •  無奈伤痛
    2021-01-21 01:01

    The one you want is CellContentClick, on the DGV itself. Attach a handler that checks to see if that column of the DGV is a CheckBoxCell, and if so, uncheck all other checkboxes on the row.

    Just a note though, for a CheckBoxCell, this event fires before the checkbox value actually changes. This means that regardless of what you do to the current cell, it will be overridden by events that fire later. The behavior that will shake out of this is that you can have none of the cells on a row checked, by checking one box on the row and then checking it again (whether you try to set the checkbox value in the handler or not, the checkbox will end up cleared after the second click). To overcome that and force one of the checkboxes to be checked, you can handle CellValueChanged instead, and if the cell that changed is the current cell and is unchecked, check it.

提交回复
热议问题