checkbox - c# HeaderCheckbox in datagridview strange behaviour -


i have got header checkbox selectall in checkboxcolumn of datagridview

    private void addselectallcheckbox(datagridview thedatagridview)     {         checkbox cbx = new checkbox();         cbx.name = "selectall";         //the box size         cbx.size = new size(14, 14);          rectangle rect = default(rectangle);         rect = thedatagridview.getcelldisplayrectangle(0, -1, true);         //put checkbox in middle-center of column header.         cbx.location = new system.drawing.point(rect.location.x + ((rect.width - cbx.width) / 2), rect.location.y + ((rect.height - cbx.height) / 2));         cbx.backcolor = color.white;         thedatagridview.controls.add(cbx);          //handle header checkbox check/uncheck function         cbx.click += headercheckbox_click;         //when checkbox value in datagridviewrows changed,         //check/uncheck header checkbox accordingly.         //thedatagridview.cellvaluechanged += datagridview_cellchecked;         //this event handler necessary commit new checkbox cell value right after         //user clicks checkbox.         //without it, cellvaluechanged event occurs until checkbox cell lose focus         //which means header checkbox won't display corresponding checked state instantly when user         //clicks 1 of checkboxes.         thedatagridview.currentcelldirtystatechanged += datagridview_currentcelldirtystatechanged;     }      private void headercheckbox_click(object sender, eventargs e)     {         this._isselectallchecked = true;          checkbox cbx = default(checkbox);         cbx = (checkbox)sender;          foreach (datagridviewrow row in metrogrid1.rows)         {              row.cells[0].value = cbx.checked;          }          metrogrid1.endedit();          this._isselectallchecked = false;     }        //the currentcelldirtystatechanged event happens after user change cell value,     //before cell lose focus , cellvaluechanged event.     private void datagridview_currentcelldirtystatechanged(system.object sender, system.eventargs e)     {         datagridview datagridview = (datagridview)sender;         if (datagridview.currentcell datagridviewcheckboxcell)         {             //when value changed cell datagridviewcheckboxcell, commit change             //to invoke cellvaluechanged event.             datagridview.commitedit(datagridviewdataerrorcontexts.commit);         }     } 

this working fine.

if check or uncheck checkbox in row event fired , works:

        private void metrogrid1_cellvaluechanged(object sender, datagridviewcelleventargs e)     {         if (e.columnindex == 0 && e.rowindex > -1)         {             //listbox1.items.add(" -> ");             datagridview dgv = sender datagridview;             if (dgv == null)                 return;             //if (dgv.currentrow)             //{                 if (convert.toboolean(dgv.currentrow.cells[sel.name].value) == true)                 {                     int selcount = int32.parse(metrolabel5.text);                     int addcount = selcount + int32.parse(dgv.currentrow.cells[jobs.name].value.tostring());                     metrolabel5.text = addcount.tostring();                     //listbox1.items.add(dgv.currentrow.index.tostring() + " -> " + dgv.currentrow.cells[jobs.name].value.tostring());                  }                 if (convert.toboolean(dgv.currentrow.cells[sel.name].value) == false)                 {                     int selcount = int32.parse(metrolabel5.text);                     int mincount = selcount - int32.parse(dgv.currentrow.cells[jobs.name].value.tostring());                     metrolabel5.text = mincount.tostring();                    // listbox1.items.add(dgv.currentrow.index.tostring() + " -> " + dgv.currentrow.cells[jobs.name].value.tostring());                 }             //}         }     }      private void metrogrid1_oncellmouseup(object sender, datagridviewcellmouseeventargs e)     {         if (e.columnindex == 0 && e.rowindex > -1)         {             metrogrid1.endedit();         }     } 

my problem: if check or uncheck headercheckbox cellvaluechanged event fired each row ok, uses first row event.

for example: have 8 rows jobs 1,2,3,4,5,6,7,8 metrolabel1.text should 36 if selected, selectall checkbox goes 8 (8 time row 0 1 jobs)

you should use convert.toboolean(dgv.rows[e.rowindex].cells[sel.name].value) , int32.parse(dgv.rows[e.rowindex].cells[jobs.name].value.tostring())

the event fires each cell belongs different row, when want value row, should refer row event have been fired for:

dgv.rows[e.rowindex].cells["column name"].value 

now in code, getting value dgv.currentrow. not need.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -