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...

  1. use cellcontentclick instead of cellclick. checkbox value triggered change in former.
  2. use currentcell instead of current row cells[0], otherwise you're unnecessarily triggering code if clicked in different cell checkboxcell in same row.
  3. if checkboxcell clicked (checked/unchecked), iterate through rows count number of checked cells in correct column. cell.value can null, true, or false , clicked cell not yet reflect new value. instead, use cell.editedformattedvalue, have updated values , true or false.

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

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -