Key code:
/// <Summary> /// synchronize the checkbox status of the column header and the checkbox status in the column /// </Summary> /// <Param name = "View"> gridview </Param> /// <Param name = "fieldename"> name of the checkbox column </param> /// <Param name = "E"> mouseeventargs </param> Public static void synccheckstatus (this gridview view, string fieldename, mouseeventargs e) {/* Description: * use in a mousedown event * Reference: https://www.devexpress.com/Support/Center/Question/Details/Q354489 * eg: * Private void Gvlampconfig_mousedown (Object sender, mouseeventargs e) * {* gridview _ view = sender as gridview; * _ view. synccheckstatus (gccheckfieldname, e); *} */If (E. clicks = 1 & E. button = mousebuttons. left) {view. clearsorting (); view. posteditor (); gridhitinfo _ INFO; point _ Pt = view. gridcontrol. pointtoclient (control. mouseposition); _ INFO = view. calchitinfo (_ pt); If (_ info. incolumn & _ info. column. field Name. equals (fieldename) {If (getcheckedcount (view, fieldename) = view. datarowcount) unchekall (view, fieldename); else checkall (view, fieldename) ;}} Private Static int getcheckedcount (gridview view, string filedname) {int COUNT = 0; for (INT I = 0; I <view. datarowcount; I ++) {object _ cellvalue = view. getrowcellvalue (I, view. columns [filedname]); // If (_ cellvalue! = NULL &&! (_ Cellvalue is dbnull) if (_ cellvalue = NULL) continue; If (string. isnullorempty (_ cellvalue. tostring (). trim () continue; bool _ checkstatus = false; If (bool. tryparse (_ cellvalue. tostring (), Out _ checkstatus) {// If (bool) _ cellvalue) if (_ checkstatus) Count ++ ;}} return count ;} private Static void checkall (gridview view, string fieldname) {for (INT I = 0; I <view. datarowcount; I ++) {view. setrowcellvalue (I, view. columns [fieldname], true) ;}} Private Static void unchekall (gridview view, string fieldname) {for (INT I = 0; I <view. datarowcount; I ++) {view. setrowcellvalue (I, view. columns [fieldname], false );}}
Code usage:
private void gvLampConfig_MouseDown(object sender, MouseEventArgs e) { GridView _view = sender as GridView; _view.SyncCheckStatus(gcCheckFieldName, e); }
Code effect: