C. add check box to the header of datagirdview column and select multiple columns

This is the code for the class:
public delegate void CheckBoxClickedHandler(bool state);
public class DataGridViewCheckBoxHeaderCellEventArgs : EventArgs
{
bool _bChecked;
public DataGridViewCheckBoxHeaderCellEventArgs(bool bChecked)
{
_bChecked = bChecked;
}
public bool Checked
{
get { return _bChecked; }
}
}
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);
    }     
}

After instantiation, the
DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
DatagridViewCheckBoxHeaderCell cbHeader = new DatagridViewCheckBoxHeaderCell();
colCB.HeaderCell = cbHeader;
datagridview1.Columns.Add(colCB);
cbHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);
1. We only need to define a DataGridViewCheckBoxColumn.
2. Then define a CheckboxClicked event for the checkbox of each line.
Test program
Create a Winform project, add a datagridview control, and initialize several lines of default data. Note: datagirdview has editing status. If there is a row of data in editing status, the row will be edited.
The solution is to add the EndEdit() call to the event binding method.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

        InitDtSource();
    }

    private void cbHeader_OnCheckBoxClicked(bool state)
    {
        //This sentence is very important
        dgInfo.EndEdit();
        dgInfo.Rows.OfType<DataGridViewRow>().ToList().ForEach(t => t.Cells[0].Value = state);
    }

    private void InitDtSource()
    {
        try
        {
            var _dtSource = new DataTable();
            //1. Add column
            _dtSource.Columns.Add("Full name", typeof(string)); //Data type is text
            _dtSource.Columns.Add("ID number", typeof(string)); //Data type is text
            _dtSource.Columns.Add("time", typeof(string)); //Data type is text
            _dtSource.Columns.Add("place", typeof(string)); //Data type is text

            for (int i = 0; i < 10; i++)
            {
                DataRow drData = _dtSource.NewRow();
                drData[0] = "test" + i;
                drData[1] = "35412549554521263" + i;
                drData[2] = "2017-05-21 10:55:21";
                drData[3] = "Beijing City";
                _dtSource.Rows.Add(drData);
            }

            dgInfo.DataSource = _dtSource;

            InitColumnInfo();
        }
        catch (Exception ex)
        {

        }
    }

    private void InitColumnInfo()
    {
        int index = 0;

        DataGridViewCheckBoxColumn colCB = new DataGridViewCheckBoxColumn();
        DatagridViewCheckBoxHeaderCell cbHeader = new DatagridViewCheckBoxHeaderCell();
        colCB.HeaderCell = cbHeader;
        colCB.HeaderText = "All election";
        cbHeader.OnCheckBoxClicked += new CheckBoxClickedHandler(cbHeader_OnCheckBoxClicked);
        dgInfo.Columns.Insert(index, colCB);


        index++;
        dgInfo.Columns[index].HeaderText = "Full name";
        dgInfo.Columns[index].Width = 90;

        index++;
        dgInfo.Columns[index].HeaderText = "ID number";
        dgInfo.Columns[index].Width = 120;

        index++;
        dgInfo.Columns[index].HeaderText = "time";
        dgInfo.Columns[index].Width = 150;

        index++;
        dgInfo.Columns[index].HeaderText = "place";
        dgInfo.Columns[index].Width = 100;

        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;
        dgInfo.Columns[index].DefaultCellStyle = dataGridViewCellStyle2;
    }
}

Reprints: https://www.cnblogs.com/zhangweizhong/p/6885507.html
Pro test available

Keywords: Windows

Added by newman on Tue, 31 Dec 2019 14:28:25 +0200