winforms - How to Count Checked CheckBoxes count in DataGridView in WindowsForms Application -
i have datagridview.in checkbox column there.if want check check box in datagrid view 1 button visible if no checkbox selected button enabled , if selected more 5 checkboxes 1 caution appered tryed
private void gridview1_cellclick(object sender, datagridviewcelleventargs e) { datagridviewcheckboxcell ch1 = new datagridviewcheckboxcell(); ch1 = (datagridviewcheckboxcell)gridview1.rows[gridview1.currentrow.index].cells[0]; if (ch1.value == null) { btnshow.visible = false; } else btnshow.visible = true; }
here did not exact out put.how can solve pls help...
- use
cellcontentclick
instead ofcellclick
.checkbox
value triggered change in former. - use
currentcell
instead of current rowcells[0]
, otherwise you're unnecessarily triggering code if clicked in different cellcheckboxcell
in same row. - if
checkboxcell
clicked (checked/unchecked), iterate through rows count number of checked cells in correct column.cell.value
cannull
,true
, orfalse
, clicked cell not yet reflect new value. instead, usecell.editedformattedvalue
, have updated values ,true
orfalse
.
for example:
private void datagridview1_cellcontentclick(object sender, datagridviewcelleventargs e) { if ((datagridview1.currentcell datagridviewcheckboxcell) != null) { int count = 0; foreach (datagridviewrow row in datagridview1.rows) { bool ischecked = (bool)row.cells[0].editedformattedvalue; if (ischecked) { count++; } } btnshow.visible = count > 0; // whatever condition may be. if (count > 5) { // caution here. example: messagebox.show(this, "danger, robinson!", "caution"); } } }
Comments
Post a Comment