[DevExpress]GridControl 同步列頭checkbox與列中checkbox狀態

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   width   

關鍵代碼:

        /// <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 = 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);            }        }

代碼使用:

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

代碼效果:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.