[Devexpress] gridcontrol synchronizes the checkbox status of the column header and the checkbox status in the column

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.