博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
啥?客户叫在DataGridView的左上角添加CheckBox?
阅读量:7122 次
发布时间:2019-06-28

本文共 4354 字,大约阅读时间需要 14 分钟。

效果图是这样的,如何把CheckBox放到左上角是最重要的。

 

添加

class DatagridViewCheckBoxHeaderCell : DataGridViewColumnHeaderCell        {            Point checkBoxLocation;            Size checkBoxSize;            bool _checked = false;            Point _cellLocation = new Point();            System.Windows.Forms.VisualStyles.CheckBoxState _cbState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;            public event CheckBoxClickedHandler OnCheckBoxClicked;            public DatagridViewCheckBoxHeaderCell()            {            }            protected override void Paint(System.Drawing.Graphics graphics,                System.Drawing.Rectangle clipBounds,                System.Drawing.Rectangle cellBounds,                int rowIndex,                DataGridViewElementStates dataGridViewElementState,                object value,                object formattedValue,                string errorText,                DataGridViewCellStyle cellStyle,                DataGridViewAdvancedBorderStyle advancedBorderStyle,                DataGridViewPaintParts paintParts)            {                base.Paint(graphics, clipBounds, cellBounds, rowIndex,                    dataGridViewElementState, value,                    formattedValue, errorText, cellStyle,                    advancedBorderStyle, paintParts);                Point p = new Point();                Size s = CheckBoxRenderer.GetGlyphSize(graphics,                System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);                p.X = cellBounds.Location.X +                    (cellBounds.Width / 2) - (s.Width / 2);                p.Y = cellBounds.Location.Y +                    (cellBounds.Height / 2) - (s.Height / 2);                _cellLocation = cellBounds.Location;                checkBoxLocation = p;                checkBoxSize = s;                if (_checked)                    _cbState = System.Windows.Forms.VisualStyles.                        CheckBoxState.CheckedNormal;                else                    _cbState = System.Windows.Forms.VisualStyles.                        CheckBoxState.UncheckedNormal;                CheckBoxRenderer.DrawCheckBox                (graphics, checkBoxLocation, _cbState);            }            protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)            {                Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);                if (p.X >= checkBoxLocation.X && p.X <=                    checkBoxLocation.X + checkBoxSize.Width                && p.Y >= checkBoxLocation.Y && p.Y <=                    checkBoxLocation.Y + checkBoxSize.Height)                {                    _checked = !_checked;                    if (OnCheckBoxClicked != null)                    {                        OnCheckBoxClicked(_checked);                        this.DataGridView.InvalidateCell(this);                    }                }                base.OnMouseClick(e);            }        }

 

添加方法 InitColumnInfo() 方法,代码如下。

private void InitColumnInfo()        {            int index = 0;            DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();            DatagridViewCheckBoxHeaderCell cbHeader = new DatagridViewCheckBoxHeaderCell();            colCB.HeaderCell = cbHeader;            colCB.HeaderText = "";            colCB.FillWeight = 50;            cbHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);            dataGridView2.Columns.Insert(index, colCB);            System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();            dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;//211, 223, 240            dataGridViewCellStyle2.ForeColor = System.Drawing.Color.Blue;            dataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.Blue;            dataGridView2.Columns[index].DefaultCellStyle = dataGridViewCellStyle2;        }

在Clicked事件中添加代码

private void cbHeader_OnCheckBoxClicked(bool state)        {            //这一句很重要结束编辑状态            dataGridView2.EndEdit();            dataGridView2.Rows.OfType
().ToList().ForEach(t => t.Cells[0].Value = state); }

最后在窗体的Load事件中,调用 InitColumnInfo() 方法就可以了。

转载于:https://www.cnblogs.com/ZaraNet/p/10749575.html

你可能感兴趣的文章
IMF总裁:科技变革对于就业的意义何在
查看>>
突破性能瓶颈 东芝发布全新固态硬盘
查看>>
大数据助力农牧业转型升级
查看>>
90亿赔偿没戏了:法官拒甲骨文重审Android侵权案要求
查看>>
智能LED路灯有多种应用功能 将成为智慧城市发展的突破口
查看>>
论大数据对媒体融合的推进作用
查看>>
微软黑科技:DNA存储技术催生方糖大小的数据中心
查看>>
招聘新手段:录段语音来判断求职者是否适合岗位
查看>>
倪光南:大数据安全问题重要性远超数据安全
查看>>
应对网络威胁 有望不再被动“打补丁”
查看>>
老国企如何焕发新势能?致远互联“协同五环”锻造老而弥坚
查看>>
被戴尔收购的EMC宣布明年裁员:人数未定
查看>>
物联网时代MCU 将迎来三大发展趋势
查看>>
解决最后一米信号问题飞鱼星VF-E300全新上市
查看>>
智慧城市安全问题初探
查看>>
打造NFV环境下的专属性能
查看>>
测试用例编写规范
查看>>
SWIFT系统第三家银行曝遭网络劫匪抢走1200万美元
查看>>
Java的GC机制
查看>>
espresso系列3--测试实践
查看>>