[Devexpress] Create a checkbox in the gridcontrol column header

Source: Internet
Author: User

Key code:

/// <Summary> /// draw checkbox for the column header // </Summary> /// <Param name = "View"> gridview </param> /// <Param name = "checkitem"> repositoryitemcheckedit </param> /// <Param name = "fieldname"> name of the checkbox column to be drawn </param> /// <Param name = "E"> columnheadercustomdraweventargs </param> Public static void drawheadercheckbox (this gridview view, repositoryitemcheckedit checkitem, string fieldname, columnheadercustomdraweventargs E) {/* Description: * Reference: https://www.devexpress.com/Support/Center/Question/Details/Q354489 * In customdrawcolumnheader using * eg: * Private void gvcabchdetail_customdrawcolumnheader (Object sender, devexpress. xtragrid. views. grid. columnheadercustomdraweventargs e) * {* gridview _ view = sender as gridview; * _ view. drawheadercheckbox (checkitem, "check", e); *} */If (E. column! = NULL & E. column. fieldname. equals (fieldname) {e. info. innerelements. clear (); E. painter. drawobject (E. info); drawcheckbox (checkitem, E. graphics, E. bounds, getcheckedcount (view, fieldname) = view. datarowcount); E. handled = true ;}} Private Static void drawcheckbox (checking checkitem, graphics g, rectangle R, bool checked) {checkeditviewinfo _ INFO; checkeditpainter _ painter; controlgraphicsinfoargs _ ARGs; _ INFO = checkitem. createviewinfo () as checkeditviewinfo; _ painter = checkitem. createpainter () as checkeditpainter; _ info. editvalue = checked; _ info. bounds = r; _ info. paintappearance. forecolor = color. black; _ info. calcviewinfo (g); _ ARGs = new controlgraphicsinfoargs (_ INFO, new devexpress. utils. drawing. graphicscache (G), R); _ painter. draw (_ ARGs); _ args. cache. dispose ();} 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) continue; If (string. isnullorempty (_ cellvalue. tostring (). trim () continue; bool _ checkstatus = false; If (bool. tryparse (_ cellvalue. tostring (), Out _ checkstatus) {If (_ checkstatus) Count ++ ;}} return count ;}

Code usage:

        RepositoryItemCheckEdit CheckItem = new RepositoryItemCheckEdit();        const string gcCheckFieldName = "Checked";        private void gvLampConfig_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)        {            GridView _view = sender as GridView;            _view.DrawHeaderCheckBox(CheckItem, gcCheckFieldName, e);        }

Code effect:

Hope this is helpful!

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.