• DataGridView添加CheckBoxColumnHeader(1)


    DataGridView中没有中有checkboxcolumn,但是该column不是checkbox的列头,这让人很郁闷,关于这个问题有3种解决方法,这里介绍一种比较暴力的方法,手画checkboxcolumnheader.

    View Code
    #region datagridview列头加checkbox
    public 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()
    {
    }

    public bool Checked
    {
    get { return _checked; }
    set
    {
    _checked
    = value;
    //OnCheckBoxClicked(_checked);
    this.DataGridView.InvalidateCell(this);
    }
    }

    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,
    "", 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);
    }
    }
    public delegate void CheckBoxClickedHandler(bool State);
    public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
    {
    bool _bChecked;
    public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked)
    {
    _bChecked
    = bChecked;
    }
    public bool Checked
    {
    get { return _bChecked; }
    }
    }
    #endregion

      
    //窗体运行时更新DataGridView
      public void Form_Load()
      {
        DatagridViewCheckBoxHeaderCell dgvcCheckBox
    = new DatagridViewCheckBoxHeaderCell();
        dgvcCheckBox.OnCheckBoxOnClicked
    += new CheckBoxClickedHandler(HeaderCellChecked);//注册列头的checkbox选中事件
        dgvDataGridVew.Columns["Checked"].HeaderCell = dgvcCheckBox;//设置checkbox列列头cell为我们画的那个checkbox列头
      }
      
    public void HeaderCellChecked(object sender, DataGridViewCheckBoxHeaderCellEventArgs e)
      {
        
    //代码
      }

  • 相关阅读:
    伪静态常用配置方法
    vue移动端选择日期,星期,时间控件
    didact笔记
    OpenSSL 安装与配置(转)
    2、Servlet系列jsp
    24、lambda表达式
    20、File类、io流(字节流\字符流\输入流\输出流\标准输入\标准输出\打印流)
    17、集合框架_HashSet\TreeSet\比较器\泛型
    23、网络编程 IP、TCP、UDP
    21、多线程(线程与进程\线程的实现方式\线程的声明周期\线程同步与死锁\生产者消费者问题)
  • 原文地址:https://www.cnblogs.com/WindBlog/p/2085210.html
Copyright © 2020-2023  润新知