How to show checkbox in DataGridView

xkullxandy

Member
Joined
Mar 21, 2011
Messages
13
Programming Experience
Beginner
Hello all, anybody knows how to show checkbox in data grid view so if we click it, the row will be selected. Help please. Thank you
 
You can add a check box column to your grid in the designer. You can then handle the CellContentClick event of the grid to be notified when the user checks or unchecks the box. The 'e' parameter in that event handler will give you the ColumnIndex of the cell, so you can test whether it was the check box column or not. If it was then 'e' will also give you the RowIndex. Using the RowIndex, you can get the row and then, using the ColumnIndex, you can get the cell. You can then get the Value of the cell and set the Selected property of the row accordingly.

There's a bit of a gotcha there though. The CellContentClick event is raised when the user clicks the box in a cell but the Value of the cell doesn't actually change until focus moves to another cell. As such, you'll be looking for the opposite value to what you might think, i.e. if Value is False then Selected should be True and vice versa.

There's another slight issue too. If you check a box to select a row, the user might then unselect it without clicking the box and, unless you write code to do so, the box will remain checked even though the row is not selected.
 
sorry,i don't understand it quite well, do you mean just add checkbox control inside the datagridview? it is like this, i would like to have checkbox on every rows as the first column and when user gives check to the check box then the row's background color turns into different color
 
No, I mean add a check box column to the grid in the designer. Start by clicking on the grid's smart tag, which is the little arrow in a square at the top, right-hand corner of the control. Note that you have to select the control for it to appear.
 
yes, thanks, i've add new first column contains check box for every rows, now if i would like the rows which have been checked to be updated, how can i do that ?
 
the check box can be checked if Enable Editing mode is enabled, right ? but it makes the other columns can also be edited, how can i make the only column that can be edited is checkbox column ? i've tried many ways and have no clue. Thanks
 
Back
Top