DataGridView自定义列标题内容(复选框控件)

编程入门 行业动态 更新时间:2024-10-27 17:23:18
本文介绍了DataGridView自定义列标题内容(复选框控件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可以向WinForms添加功能全部检查DataGridView 的DataGridViewCheckBoxColumn ?

Is it possible to add functionality "check all" to WinForms DataGridView's DataGridViewCheckBoxColumn?

它应该如下所示:

点击highlited复选框应该检查/取消选中网格中的所有复选框。

Click on highlited checkbox should check/uncheck all checkboxes in the grid.

如我所见,列标题只能包含字符串值。有没有解决方法?

As I can see, column header can contain only string values. Is there any workaround?

推荐答案

最终实现主要是解决方案,由Samir在这篇文章。

Final implementation is mostly solution, proposed by Samir in this article.

但是当网格水平滚动条移动时,它需要修复复选框位置。所以这里需要修改的方法:

But it requires fix of checkbox position when grid horizontal scrollbar is moving. So here follows methods that needed to be changed:

private void frmSelectAll_Load(object sender, EventArgs e) { AddHeaderCheckBox(); HeaderCheckBox.KeyUp += new KeyEventHandler(HeaderCheckBox_KeyUp); HeaderCheckBox.MouseClick += new MouseEventHandler(HeaderCheckBox_MouseClick); dgvSelectAll.CellValueChanged += new DataGridViewCellEventHandler(dgvSelectAll_CellValueChanged); dgvSelectAll.CurrentCellDirtyStateChanged += new EventHandler(dgvSelectAll_CurrentCellDirtyStateChanged); dgvSelectAll.CellPainting += new DataGridViewCellPaintingEventHandler(dgvSelectAll_CellPainting); BindGridView(); var checkboxHeaderCellRect = dgvSelectAll.GetCellDisplayRectangle(0, -1, false); headerCheckboxRightMargin = (checkboxHeaderCellRect.Width - HeaderCheckBox.Width)/2; } private int headerCheckboxRightMargin; private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex) { //Get the column header cell bounds Rectangle oRectangle = this.dgvSelectAll.GetCellDisplayRectangle(ColumnIndex, RowIndex, false); Point oPoint = new Point(); oPoint.X = oRectangle.Location.X + (oRectangle.Width - headerCheckboxRightMargin - HeaderCheckBox.Width); oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1; if (oPoint.X < oRectangle.X) { HeaderCheckBox.Visible = false; } else { HeaderCheckBox.Visible = true; } //Change the location of the CheckBox to make it stay on the header HeaderCheckBox.Location = oPoint; }

更多推荐

DataGridView自定义列标题内容(复选框控件)

本文发布于:2023-11-07 09:00:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1566044.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   控件   复选框   标题   内容

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!