首页 > 代码库 > [DevExpress]GridControl 列头绘制Checkbox
[DevExpress]GridControl 列头绘制Checkbox
关键代码:
/// <summary> /// 为列头绘制CheckBox /// </summary> /// <param name="view">GridView</param> /// <param name="checkItem">RepositoryItemCheckEdit</param> /// <param name="fieldName">需要绘制Checkbox的列名</param> /// <param name="e">ColumnHeaderCustomDrawEventArgs</param> public static void DrawHeaderCheckBox(this GridView view, RepositoryItemCheckEdit checkItem, string fieldName, ColumnHeaderCustomDrawEventArgs e) { /*说明: *参考:https://www.devexpress.com/Support/Center/Question/Details/Q354489 *在CustomDrawColumnHeader中使用 *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(RepositoryItemCheckEdit 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 = http://www.mamicode.com/Checked;"kwrd">newControlGraphicsInfoArgs(_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 = http://www.mamicode.com/view.GetRowCellValue(i, view.Columns[filedName]);"kwrd">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 (_checkStatus) count++; } } return count; }<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>
<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>代码使用:
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); }
代码效果:
希望有所帮助!
<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>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。