[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 = 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;
        }

.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; }

.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; }代码使用:

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

代码效果:

希望有所帮助!

.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; }

[DevExpress]GridControl 列头绘制Checkbox

时间: 2024-10-25 15:58:37

[DevExpress]GridControl 列头绘制Checkbox的相关文章

实现在DevExpress.XtraGrid.GridControl的列头绘制复选框以实现全选的功能

首先新建一个Win Form测试项目,拖一个GridControl控件到窗体上. 1 public partial class Form1 : Form 2 { 3 public Form1() 4 { 5 InitializeComponent(); 6 } 7 8 private Rectangle checkBoxColumnHeaderRect = Rectangle.Empty; 9 private GridColumn checkBoxColumn = null; 10 privat

DevExpress Gridcontrol 表格头复选框 全选全不选

新建窗体 拖一个Gridcontrol 添加两列 col1,col2 public partial class XtraForm2 : DevExpress.XtraEditors.XtraForm { public XtraForm2() { InitializeComponent(); } private void XtraForm2_Load(object sender, EventArgs e) { //设置复选框 gridView1.OptionsSelection.MultiSele

DevExpress GridControl 列中显示图片

一.GridControl 的Columns中添加列 1.列名:FieldName命名为img 2.类型:ColumnEdit属性中 选择PictureEdit类型(RepositoryItemPictureEdit) 二.GridControl绑定的数据,不管是DataTable.List或者其他源,添加一个列,列名为img. 以DataTable为例: void bindGrid01() { DataTable dt = new DataTable(); dt.Columns.Add(new

给GridView添加列头复选框

1 using DevExpress.XtraEditors.Drawing; 2 using DevExpress.XtraEditors.Repository; 3 using DevExpress.XtraEditors.ViewInfo; 4 using DevExpress.XtraGrid.Views.Grid; 5 using System; 6 using System.Collections.Generic; 7 using System.Drawing; 8 using Sy

WPF DataGrid DataGridTemplateColumn 列头checkbox如何在代码中取消选择

0 登录进行投票 <DataGrid Name="DG">                <DataGrid.Columns>                    <DataGridTemplateColumn Width="70">                        <DataGridTemplateColumn.HeaderTemplate>                            &l

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

关键代码: /// <summary> /// 同步列头checkbox与列中checkbox状态 /// </summary> /// <param name="view">GridView</param> /// <param name="fieldeName">需要绘制Checkbox的列名</param> /// <param name="e">Mouse

GridControl实现多选和列头全选

大名鼎鼎的DEV啊……弄了一个gridControl结果不知道如何加选择列,还有表头全选上网搜了搜终于搜到解决方案……结果解决办法麻烦的要死…… 这么一个挺基础的需求,DEV不提供解决方案?这没道理吧?怎么办啊……就差这一个功能了……换控件不划算啊 有这么一个方法来完成(只适合winform): 1.     绑定数据的源表添加一列      DataTable dt = ds.Tables[0]; dt.Columns.Add("selected", System.Type.GetT

gridcontrol的列头右键菜单问题

Dev控件GridControl设置了一个右键菜单 this.gridControl1.ContextMenu = contextMenu2; 而GridControl在运行排序的时候,即 gridview1->OptionsCustomization->AllowSort设为true时,控件自带一个英文的右键菜单如下 这样当点击表列头时,就会出现下面的两个右键菜单重叠问题. 这样当点击表列头时,就会出现下面的两个右键菜单重叠问题. 解决办法: privatevoid gridView1_Mo

Winfrom DataGridView添加列头checkbox

using System; using System.Data; using System.Drawing; using System.Windows.Forms; using Yanwen.Logistics.Business.Logics; using Yanwen.Logistics.Business.Models.ExpressTransport; using Yanwen.Logistics.Desktop.Extensions; namespace Yanwen.Logistics.