首页 > 代码库 > [DevExpress]GridControl 同步列头checkbox与列中checkbox状态

[DevExpress]GridControl 同步列头checkbox与列中checkbox状态

关键代码:

        /// <summary>        /// 同步列头checkbox与列中checkbox状态        /// </summary>        /// <param name="view">GridView</param>        /// <param name="fieldeName">需要绘制Checkbox的列名</param>        /// <param name="e">MouseEventArgs</param>        public static void SyncCheckStatus(this GridView view, string fieldeName, MouseEventArgs e)        {            /*说明:             *在MouseDown事件中使用             *参考: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.FieldName.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 = http://www.mamicode.com/view.GetRowCellValue(i, view.Columns[filedName]);"rem">//if (_cellValue != null && !(_cellValue is DBNull))                if (_cellValue =http://www.mamicode.com/= 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);            }        }

代码使用:

        private void gvLampConfig_MouseDown(object sender, MouseEventArgs e)        {            GridView _view = sender as GridView;            _view.SyncCheckStatus(gcCheckFieldName, e);        }

<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>代码效果:

image