GridControl 选择列、复选框全选

说明:

GirdControl 中添加一列,这一列不是写在数据库中的,而是代码中添加的。

图示:

底层类代码:

#region GridControl 全选
        /// <summary>
        /// 是否选中
        /// </summary>
        private static bool chkState = false;
        //复选框列名称
        private static string chkFileName = "";
        //复选框列宽
        private static int chkWidth = 30;
        //GridView
        public static DevExpress.XtraGrid.Views.Grid.GridView GView = null;
        private DevExpress.XtraGrid.Views.Grid.GridView gView {
            get {
                if (GView == null)
                {
                    GView = new DevExpress.XtraGrid.Views.Grid.GridView();
                }
                return GView;
            }
            set {
                this.gView = value;
            }
        } 

        public static void GridCheckEdit(DevExpress.XtraGrid.Views.Grid.GridView gv, string checkFileName, int checkWidth)
        {
            if (gv != null) {
            chkFileName = checkFileName;
            chkWidth = checkWidth;
            GView = gv;
            //不显示复选框的列标题
            gv.Columns[chkFileName].OptionsColumn.ShowCaption = false;

            //复选框的形状   gv.Columns[chkFileName].ColumnEdit 实例是 repositoryItemCheckEdit1
            //repositoryItemCheckEdit1.CheckStyle = DevExpress.XtraEditors.Controls.CheckStyles.Standard;
            ////复选框加载的状态     实心   空心   空心打勾
            //repositoryItemCheckEdit1.NullStyle = DevExpress.XtraEditors.Controls.StyleIndeterminate.Unchecked;
            //点击事件
            gv.Click += new System.EventHandler(gv_Click);
            //画列头CheckEdit
            gv.CustomDrawColumnHeader += new DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventHandler(gv_CustomDrawColumnHeader);
            gv.DataSourceChanged += new EventHandler(gv_DataSourceChanged);
            }
        }

        private static void gv_Click(object sender, EventArgs e)
        {
            if (ClickGridCheckBox(GView, chkFileName, chkState))
            {
                chkState = !chkState;
            }
        }

        private static void gv_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)
        {
            if (e.Column != null && e.Column.FieldName ==chkFileName)
            {
                e.Info.InnerElements.Clear();
                e.Painter.DrawObject(e.Info);
                DrawCheckBox(e, chkState);
                e.Handled = true;
            }
        }

        private static void gv_DataSourceChanged(object sender, EventArgs e)
        {
            DevExpress.XtraGrid.Columns.GridColumn column = GView.Columns.ColumnByFieldName(chkFileName);
            if (column != null)
            {
                column.Width = chkWidth;
                column.OptionsColumn.ShowCaption = false;
                column.ColumnEdit = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();
            }
        }

        private static void DrawCheckBox(DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e, bool chk)
        {
           DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit repositoryCheck = e.Column.ColumnEdit as DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit;
            if (repositoryCheck != null)
            {
                System.Drawing.Graphics g = e.Graphics;
                System.Drawing.Rectangle r = e.Bounds;

                DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info;
                DevExpress.XtraEditors.Drawing.CheckEditPainter painter;
                DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;
                info = repositoryCheck.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;

                painter = repositoryCheck.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter;
                info.EditValue = chk;
                info.Bounds = r;
                info.CalcViewInfo(g);
                args = new DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
                painter.Draw(args);
                args.Cache.Dispose();
            }
        }

        private static bool ClickGridCheckBox(DevExpress.XtraGrid.Views.Grid.GridView gridView, string fieldName, bool currentStatus)
        {
            bool result = false;
            if (gridView != null)
            {
                //禁止排序
                gridView.ClearSorting();

                gridView.PostEditor();
                DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info;
                System.Drawing.Point pt = gridView.GridControl.PointToClient(Control.MousePosition);
                info = gridView.CalcHitInfo(pt);
                if (info.InColumn && info.Column != null && info.Column.FieldName == fieldName)
                {
                    for (int i = 0; i < gridView.RowCount; i++)
                    {
                        gridView.SetRowCellValue(i, fieldName, !currentStatus);
                    }
                    return true;
                }
            }
            return result;
        } 

        #endregion

前台调用:

注意:GridControl绑定前,手动增加一列,增加完成之后,在绑定。

   //添加一列
            dt.Columns.Add("chk", System.Type.GetType("System.Boolean"));
            dt.Columns["chk"].DefaultValue = Boolean.FalseString;

            gridControl1.DataSource = dt;

            Functionjsj.GridCheckEdit(gv, "chk", 50);

chk 图例:

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-30 11:32:54

GridControl 选择列、复选框全选的相关文章

jQuery实现复选框全选/全部取消/反选/获得选择的值

本地单文件上传脚本,命名uf 这是在本机上做的测试,利用bpcs_uploader脚本实现,只是进行简单的封装,自动完善云端文件路径. 技术要点:使用dirname获取文件所在目录,使用pwd获取文件完整路径,并作为云端文件路径. #!/bin/bash cur_dir=$(cd "$(dirname "$1")"; pwd) name=$(basename "$1") /home/grm/bin/bpcs_uploader/bpcs_uploa

jQuery 复选框全选/取消全选/反选

jQuery实现的复选框全选/取消全选/反选及获得选择的值. 完整代码: <!DOCTYPE html> <html> <head> <script type="text/javascript" src="../js/jquery-1.9.1.js"></script> <script type="text/javascript"> $(document).ready(fun

jQuery如何实现复选框全选和全不选

jQuery如何实现复选框全选和全不选: 在网页中经常有复选框的全选和全不选效果,比如在后台新闻管理.用户空间信息管理等都有用到,下面就提供一个jQuery实现的此效果.代码实例如下: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name=&quo

checkbox复选框全选批量删除

多选框全选实现批量删除 html代码 <body> <form action="" method="post" name="FormName" onsubmit="return checkbox();"> <table > <tr ><td><input type="checkbox" name="checkboxes[]"

jQuery复选框全选全不选代码

<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="author" content="http://www.51texiao.cn/" /><title>jQuery复选框全选全不选代码<

checkbox全选/全不选,子复选框全选父复选框选中

<input type="checkbox" class="optionListAll">/* 父复选框 */ <input type="checkbox" name="optionList"> <input type="checkbox" name="optionList"> <input type="checkbox" n

html+css+js实现复选框全选与反选

1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2 <html> 3 <head> 4 <title>html+css+js实现复选框全选与反选</title> 5 <meta http-equiv="content-type&qu

javascript实现复选框全选和取消代码分析

javascript实现复选框全选和取消代码分析:复选框是常用的元素之一,而点击实现全选和取消全选又是最为常用的功能,特别是在批量操作管理中,非常的方便,下面就通过代码实例介绍一下如何实现此效果,代码实例如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.soft

复选框全选、全不选和反选的效果实现

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>石家庄车库棚|tongtianty.net---复选框全选.全不选和反选的效果实现</title> <SCRIPT LANGUAGE="JavaScript"> <!-- Begin func

jQuery实现的checkbox复选框全选和全不选效果

jQuery实现的checkbox复选框全选和全不选效果:复选框的全选和全不选效果在代码中非常的常用,尤其在批量处理的需求中更是如此,下面就通过一个代码实例简单介绍一下如何实现此效果,希望能够对需要的朋友有所帮助,代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http: