DevExpress实现GridControl同步列头checkbox与列中checkbox状态

时间:2021-05-20

本文实例展示了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:///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);}

代码运行效果如下:

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章