Winfrom DataGridView添加列头checkbox

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Yanwen.Logistics.Business.Logics;
using Yanwen.Logistics.Business.Models.ExpressTransport;
using Yanwen.Logistics.Desktop.Extensions;

namespace Yanwen.Logistics.Desktop.Transport
{
    public partial class TransportLanshouForm : Form
    {
       public TransportLanshouForm()
        {
            InitializeComponent();
        }
        //显示行号
        private void gdvData_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            var b = new SolidBrush(gdvData.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), gdvData.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
private void TransportLanshouForm_Load(object sender, EventArgs e)
        {
            var ch = new DatagridviewCheckboxHeaderCell();
            ch.OnCheckBoxClicked += new DatagridviewcheckboxHeaderEventHander(ch_OnCheckBoxClicked);//关联单击事件
            var checkboxCol = this.gdvData.Columns[0] as DataGridViewCheckBoxColumn;
            checkboxCol.HeaderCell = ch;
            checkboxCol.HeaderCell.Value = string.Empty;//消除列头checkbox旁出现的文字
        }

        /// <summary>
        /// 单击事件
        /// </summary>
        private void ch_OnCheckBoxClicked(object sender, DatagridviewCheckboxHeaderEventArgs e)
        {
            foreach (DataGridViewRow dgvRow in this.gdvData.Rows)
            {
                dgvRow.Cells[0].Value = e.CheckedState;
            }
        }
    }

    //定义触发单击事件的委托
    public delegate void DatagridviewcheckboxHeaderEventHander(object sender, DatagridviewCheckboxHeaderEventArgs e);

    //定义包含列头checkbox选择状态的参数类
    public class DatagridviewCheckboxHeaderEventArgs : EventArgs
    {
        public DatagridviewCheckboxHeaderEventArgs()
        {
            CheckedState = false;
        }

        public bool CheckedState { get; set; }
    }

    //定义继承于DataGridViewColumnHeaderCell的类,用于绘制checkbox,定义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 DatagridviewcheckboxHeaderEventHander OnCheckBoxClicked;

        //绘制列头checkbox
        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) - 1;//列头checkbox的X坐标
            p.Y = cellBounds.Location.Y +
                (cellBounds.Height / 2) - (s.Height / 2);//列头checkbox的Y坐标
            _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);
        }

        /// <summary>
        /// 点击列头checkbox单击事件
        /// </summary>
        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {

            var 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;

                //获取列头checkbox的选择状态
                var ex = new DatagridviewCheckboxHeaderEventArgs { CheckedState = _checked };

                var sender = new object();//此处不代表选择的列头checkbox,只是作为参数传递。应该列头checkbox是绘制出来的,无法获得它的实例
                if (OnCheckBoxClicked != null)
                {
                    OnCheckBoxClicked(sender, ex);//触发单击事件
                    this.DataGridView.InvalidateCell(this);
                }
            }
            base.OnMouseClick(e);
        }

    }

}
时间: 2024-10-20 00:53:12

Winfrom DataGridView添加列头checkbox的相关文章

DataGrid列头 checkBox 绑定方式

<DataGridTemplateColumn.HeaderTemplate> <DataTemplate> <CheckBox x:Name="chkSelectAll" HorizontalAlignment="Center" VerticalAlignment="Center" Click="SelectedAll_Checked" IsChecked="{Binding Rela

[DevExpress]GridControl 同步列头checkbox与列中checkbox状态

关键代码: /// <summary> /// 同步列头checkbox与列中checkbox状态 /// </summary> /// <param name="view">GridView</param> /// <param name="fieldeName">需要绘制Checkbox的列名</param> /// <param name="e">Mouse

WPF DataGrid DataGridTemplateColumn 列头checkbox如何在代码中取消选择

0 登录进行投票 <DataGrid Name="DG">                <DataGrid.Columns>                    <DataGridTemplateColumn Width="70">                        <DataGridTemplateColumn.HeaderTemplate>                            &l

给GridView添加列头复选框

1 using DevExpress.XtraEditors.Drawing; 2 using DevExpress.XtraEditors.Repository; 3 using DevExpress.XtraEditors.ViewInfo; 4 using DevExpress.XtraGrid.Views.Grid; 5 using System; 6 using System.Collections.Generic; 7 using System.Drawing; 8 using Sy

dataGridView添加列行

//全局设置文本对齐方式. dataGridView1.RowsDefaultCellStyle.Alignment= DataGridViewContentAlignment.MiddleCenter; 添加列时,必须设置列的CellTemplate,否则应该报错! 一.只添加text样式 //每一列必须设置CellTemplate //第一列 dataGridView1.Columns.Add(new DataGridViewColumn() {Name="name", Heade

[WinForm]DataGridView列头右键菜单

[WinForm]DataGridView列头右键菜单 前言 继续"不误正业" - - #,记录一下.有时候有这样的需求:DataGridView的列头菜单可以选择具体显示哪些列,而数据区域的菜单显示增删改查,这样就需要设置两个菜单了,而DataGridView并没有为列头设置菜单的选项,这里分享一段代码. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 一.拖拽一个

C#中DataGridView动态添加行及添加列的方法

http://www.jb51.net/article/72259.htm Datagridview添加列: ? 1 2 3 4 5 DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn(); acCode.Name = "acCode"; acCode.DataPropertyName = "acCode"; acCode.HeaderText = "A/C Code"

.NET组件控件实例编程系列——5.DataGridView数值列和日期列

在使用DataGridView编辑数据的时候,编辑的单元格一般会显示为文本框,逻辑值和图片会自动显示对应类型的列.当然我们自己可以手工选择列的类型,例如ComboBox列.Button列.Link列.在编辑数值和日期类型的时候,如果使用独立控件,我们会选择NumericUpDown和DataTimePicker,但在DataGridView中编辑的时候就只能用文本列.相比使用独立控件,文本框列缺少数值有效性检测,数值区间限制等功能.从本质上来看,.NET本身提供的DataGridViewChec

C++ 简单实现MFC ListControl 点击列头排序

说明: SetItemData可以为每一行绑定一个DWORD类型的变量.用GetItemData可以获得这个变量.举个例子,假设CListCtrl中你需要显示某个数据表中的记录,该表有个流水号主键ID,一般这个ID值本身没有什么意义,用户也不需要看,因此在CListCtrl的可见列中,你不需要显示.但往往做具体查询等操作时,你又需要用这个ID来完成.这时,用SetItemData将其绑定到每一行,将非常方便,用户操作哪一行,则用GetItemData可以得到对应记录的ID,直接用来做操作,很爽.