c# - Can DataGridViewCheckBoxCell accept CheckState.Indeterminate programatically, but not via clicking? -
i have followowing datagridviewcheckboxcell
part of datagridview
:
datagridviewcheckboxcell cell = new datagridviewcheckboxcell(true) {value = checkstate.unchecked}; grdrow.cells.add(cell); grdrow.tag = key; grdfilter.rows.add(grdrow);
later on, update state based on whether other check boxes checked.
var numchecked = cells.count(c => c.value.equals(true)); cbcell.value = (numchecked == cells.count) ? checkstate.checked : (numchecked == 0 ? checkstate.unchecked : checkstate.indeterminate);
this works great. however, if user clicks checkbox, cycles between checked, unchecked, , indeterminate. want cycle between checked , unchecked.
if set tristate
false
on cell, no longer allows me set value
checkstate.indeterminate
.
is there way behavior want?
update:
i've tried trapping cellvaluechanged
event so:
void datagridview_cellvaluechanged(object sender, datagridviewcelleventargs e) { var changedcell = grdfilter.rows[e.rowindex].cells[e.columnindex]; if(changedcell.value.equals(checkstate.indeterminate)) changedcell.value = checkstate.unchecked; }
however, has no effect. can still click through 3 states. i've verified event fire under debugger, , value
property indeed getting set checkstate.unchecked
.
the cellvaluechanged event think firing late catch user changing value checkbox column. try wiring currentcelldirtystatechanged event , commit change:
void dgv_currentcelldirtystatechanged(object sender, eventargs e) { dgv.commitedit(datagridviewdataerrorcontexts.commit); }
then in cellvaluechanged event, can change threestate property:
private void dgv_cellvaluechanged(object sender, datagridviewcelleventargs e) { ((datagridviewcheckboxcell)dgv.rows[e.rowindex].cells[e.columnindex]).threestate = false; }
you should filter columns on checkbox columns, etc.
Comments
Post a Comment