GridView 自带的全选表头显示文本,重绘表头和表头绑定控件

虽然我比较喜欢GridView自带的全选功能头部是checkbox,不过还是有些人比较想要用文字,作为版主,分享一下经验吧。
Dev13.x开始就有GridView的全选功能,目前也越来越完善了,13.x的版本是有问题的,当初我就放弃用自带的功能。
GridView 如果设置全选功能:
两个必要条件:
1.GridView1.OptionsSelection.MultiSelect=true;
2.GridView1.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.CheckBoxRowSelect;
当然可以可以根据自己的喜好设置其宽度:
GridView1.OptionsSelection.CheckBoxSelectorColumnWidth = 45;
 
设置后如图:

若不想显示CheckBox,而是显示文字,请将下列属性设置为false,默认显示的文字是Selection,无法设置成自定义的文字

这里有个技巧,上面的设置不要恢复默认值,在界面加载的时候再设置,不过这种方式有个前提,就是你gridView有设置列,而不是单单全选列而已,否则gridView1.VisibleColumns.Cout数量为0,无法获取全选列,更设置不了其属性了。
gridView1.OptionsSelection.ShowCheckBoxSelectorInColumnHeader = DevExpress.Utils.DefaultBoolean.False;
gridView1.VisibleColumns[0].Caption = "选择";

设效果图:

效果还是不错,不过这个虽然显示了,但是它却少了全选的功能,无法一键全选。。。
下面介绍三种方式实现既能显示文本又能一键全选功能

 

第一种:直接绘制表头text

 


第二种:在表头加个Button

第三种:在表头加个CheckEdit并显示Caption

 

 

个人觉得第三种效果会好一点.

 

 

 

private void gridView1_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)
        {
            if (e.Column != null)
            {
                if (e.Column.FieldName == "DX$CheckboxSelectorColumn")
                {
                    e.Column.OptionsColumn.AllowSize = true;
                    e.Info.InnerElements.Clear();
                    e.Painter.DrawObject(e.Info);
                    Graphics g = e.Graphics;
                    Rectangle r = e.Bounds;
                    ///方式一:直接重绘表头内容
                    //e.Column.AppearanceHeader.DrawString(new DevExpress.Utils.Drawing.GraphicsCache(g), "选择", r);
                    //e.Column.AppearanceHeader.Options.UseTextOptions = true;
                    //e.Column.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                    //e.Handled = true;
                    ///

                    ///方式二:在表头上加个button
                    //DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit repositoryButton = new DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit();
                    //repositoryButton.Buttons[0].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph;
                    //repositoryButton.Buttons[0].Caption = "选择";
                    //repositoryButton.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.HideTextEditor;
                    //repositoryButton.ButtonsStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
                    //DevExpress.XtraEditors.Drawing.ButtonEditPainter painter = repositoryButton.CreatePainter() as DevExpress.XtraEditors.Drawing.ButtonEditPainter;
                    //DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;
                    //DevExpress.XtraEditors.ViewInfo.ButtonEditViewInfo info = repositoryButton.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.ButtonEditViewInfo;
                    ///

                    ///方法三:在表头上加CheckEdit
                    DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit repositoryCheck = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();
                    repositoryCheck.GlyphAlignment = DevExpress.Utils.HorzAlignment.Default;
                    repositoryCheck.Caption = "选择";
                    DevExpress.XtraEditors.Drawing.CheckEditPainter painter = repositoryCheck.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter;
                    DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;
                    DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info = repositoryCheck.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;
                    var view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
                    if (view.RowCount == view.SelectedRowsCount&&view.DataRowCount>0)//全选状态
                        info.EditValue = true;
                    else if (view.SelectedRowsCount > 0)//非全选状态
                        info.EditValue = null;
                    else
                        info.EditValue = false;//未选状态
                    /

                    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();
                    e.Handled = true;
                }
            }
        }

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值