[Closed] DataGridView Checkbox
I can’t seem to figure out whether a checkbox in a checkbox column in a DataGridView is checked or not, it doesn’t have any property I can access that I can see. Everything seems to be working correctly (event handlers, etc), but I can’t see whether a checkbox is checked or not. If more info is needed, will provide, but I figure this shouldn’t be too difficult for anyone that knows more than I.
presuming…
dgv = the data grid view
cell = the DataGridViewCheckBoxCell (from column type DataGridViewCheckBoxColumn)
Then you can use cell.value to get the boolean state of that cell.
One word of warning – it does not reflect the user-edited state of the cell while the user is still editing the cell. The typical way of handling this is by using the events to drive any variables or states required. If you have to handle it via an outside event – say another dialog, or a rollout button or whatever – then you can first commit any edits the user may have made using dgv.commitEdit (dotNetClass “DataGridViewDataErrorContexts”).commit . There’s some quirks with this – e.g. cell.isInEditMode will happily remain true even though the UI no longer shows this, so if at all possible – handle it via the actual event.
dgv
dotNetControl:dgv:System.Windows.Forms.DataGridView
dgv.columns.item[0]
dotNetObject:System.Windows.Forms.DataGridViewCheckBoxColumn
dgv.rows.item[0].cells.item[0]
dotNetObject:System.Windows.Forms.DataGridViewCheckBoxCell
dgv.rows.item[0].cells.item[0].value
""
(I don’t know why the spacing is messed up)
Unless I’m doing something wrong, value is returning a string value… I cannot seem to access the boolean state (and as you can see, it is definitely a checkboxcell and in a checkboxcolumn).
And thanks for the warning- I gathered there’d be a gotcha from the MSDN reference, but I can’t even get the checkbox state yet!
haven’t the foggiest – try checking .valueType or, in fact, try a showProperties on the cell… that should reveal the type returned by .value as well.
The below code should work:
rollout roll_test "test" width:256 height:256 (
dotnetcontrol dnc_dgv "System.Windows.Forms.DataGridView" width:200 height:200 pos:[8,8]
button btn_printval "print value of topleft cell"
on roll_test open do (
local dno_ckb_column = dotNetObject "DataGridViewCheckBoxColumn"
dno_ckb_column.headerText = "test"
dnc_dgv.columns.add dno_ckb_column
boolVal = if ((maxVersion())[1] < 11000) then ( dotNetObject "System.Boolean" true ) else ( true )
for i = 1 to 3 do (
dnc_dgv.rows.add #(boolVal)
)
)
on btn_printval pressed do (
dnc_dgv.commitEdit (dotNetClass "DataGridViewDataErrorContexts").commit
format "Value: %
" dnc_dgv.rows.Item[0].Cells.Item[0].value
)
)
createdialog roll_test
Haha, well that’s funny enough. Where you had:
dnc_dgv.rows.add #(dotNetObject “System.Boolean” true)
I was essentially doing:
dnc_dgv.rows.add #(dotNetObject “System.String” “”)
(The rest of the row are strings, so I just set the first one (the checkbox) as an empty string). It appears setting its value as a string when you add the checkbox column messes it up, permanently (setting a cell’s value to a .net string after the fact doesn’t seem to mess it up permanently). Harumph, what a stupid bug (it should really throw an exception or just revert it to a boolean, like it does when you try to change it after the fact).
Thanks Zeboxx, for yet another snip of demo code!