how to add check box all into DataGrid view in win form?

Farshad Valizade 501 Reputation points
2023-11-15T12:10:51.0166667+00:00

Hi evet Microsoft users.

I have a code that put a checkbox all control above a DataGrid view that user can check it and all rows will be check.

Now my problem is that in every form I should copy and paste this code and it gets make a lot time.

is there any way to make a DataGrid view in each form get inheritance form this code?

private CheckBox headerCheckBox = new CheckBox();

        private void GetDG_Joint()
        {    

            //Find the Location of Header Cell.
            Point headerCellLocation = DG_Joint.GetCellDisplayRectangle(0, -1, true).Location;

            //Place the Header CheckBox in the Location of the Header Cell.
            headerCheckBox.Location = new Point(headerCellLocation.X + 5, headerCellLocation.Y + 5);
            headerCheckBox.BackColor = Color.Transparent;
            headerCheckBox.Size = new Size(18, 18);

            //Assign Click event to the Header CheckBox.
            headerCheckBox.Click += new EventHandler(HeaderCheckBox_Clicked);
            DG_Joint.Controls.Add(headerCheckBox);

            //Add a CheckBox Column to the DataGridView at the first position.
            DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
            checkBoxColumn.HeaderText = "";
            checkBoxColumn.Width = 30;
            checkBoxColumn.Name = "checkBoxColumn";
            checkBoxColumn.FalseValue = 0;
            checkBoxColumn.TrueValue = 1;
            if (!DG_Joint.Columns.Contains("checkBoxColumn"))
                DG_Joint.Columns.Insert(0, checkBoxColumn);

            //Assign Click event to the DataGridView Cell.
            DG_Joint.CellContentClick += new DataGridViewCellEventHandler(DataGridView_CellClick);

        }

        private void HeaderCheckBox_Clicked(object sender, EventArgs e)
        {
            //Necessary to end the edit mode of the Cell.

            DG_Joint.EndEdit();

            //Loop and check and uncheck all row CheckBoxes based on Header Cell CheckBox.
            foreach (DataGridViewRow row in DG_Joint.Rows)
            {
                DataGridViewCheckBoxCell checkBox = ((DataGridViewCheckBoxCell)row.Cells["checkBoxColumn"]);
                checkBox.Value = headerCheckBox.Checked;
            }
        }
        private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //Check to ensure that the row CheckBox is clicked.
            if (e.RowIndex >= 0 && e.ColumnIndex == 0)
            {
                //Loop to verify whether all row CheckBoxes are checked or not.
                bool isChecked = true;
                foreach (DataGridViewRow row in DG_Joint.Rows)
                {
                    if (Convert.ToBoolean(row.Cells["checkBoxColumn"].EditedFormattedValue) == false)
                    {
                        isChecked = false;
                        break;
                    }
                }
                headerCheckBox.Checked = isChecked;
            }
        }
        ```

Developer technologies C#
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Anonymous
    2023-11-15T14:48:35.3833333+00:00

    Hi @Farshad Valizade , Welcome to Microsoft Q&A,

    Your idea is perfectly viable, just create a custom DataGridView with this method.

    Similar code is as follows:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace _11_15_x
    {
        public partial class Customdata : DataGridView
        {
            protected CheckBox headerCheckBox = new CheckBox();
            public Customdata()
            {
                InitializeComponent();
                // Your existing code for initializing the DataGridView
                // Place the Header CheckBox bellows the bottom-right corner of the DataGridView Header.
                headerCheckBox.Location = new Point(this.Width - 18, this.Height - 18);
                headerCheckBox.BackColor = Color.Transparent;
                headerCheckBox.Size = new Size(18, 18);
    
                // Assign Click event to the Header CheckBox.
                headerCheckBox.Click += new EventHandler(HeaderCheckBox_Clicked);
                this.Controls.Add(headerCheckBox);
    
                // Add a CheckBox Column to the DataGridView at the first position.
                DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
                checkBoxColumn.HeaderText = "";
                checkBoxColumn.Width = 30;
                checkBoxColumn.Name = "checkBoxColumn";
                checkBoxColumn.FalseValue = 0;
                checkBoxColumn.TrueValue = 1;
                if (!this.Columns.Contains("checkBoxColumn"))
                    this.Columns.Insert(0, checkBoxColumn);
    
                // Assign Click event to the DataGridView Cell.
                this.CellContentClick += new DataGridViewCellEventHandler(DataGridView_CellClick);
            }
            protected void HeaderCheckBox_Clicked(object sender, EventArgs e)
            {
                // Your existing code for handling header checkbox click
            }
    
            protected void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                // Your existing code for handling cell click
            }
            protected override void OnPaint(PaintEventArgs pe)
            {
                base.OnPaint(pe);
            }
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. KOZ6.0 6,655 Reputation points
    2023-11-15T16:44:37.3466667+00:00

    It is also interesting to add columns on the form side.

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    public class CustomForm : Form
    {
        private readonly List<DataGridViewExtender> dataGridViewExtenders 
                                    = new List<DataGridViewExtender>();
    
        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            foreach (var dgv in GetAllControlOfType<DataGridView>(this)) {
                dataGridViewExtenders.Add(new DataGridViewExtender(dgv));
            }
        }
    
        private IEnumerable<T> GetAllControlOfType<T>(Control ctrl)
            where T : Control {
            if (ctrl is T target) {
                yield return target;
            } else {
                if (ctrl.HasChildren) {
                    foreach (Control child in ctrl.Controls) {
                        foreach (var dgv in GetAllControlOfType<T>(child)) {
                            yield return dgv;
                        }
                    }
                }
            }
        }
    
        private class DataGridViewExtender
        {
            private readonly CheckBox headerCheckBox = new CheckBox();
            private readonly DataGridView DG_Joint;
    
            public DataGridViewExtender(DataGridView dgv) {
                DG_Joint = dgv;
                GetDG_Joint();
            }
    
            private void GetDG_Joint() {
                // Your existing code
            }
    
            private void HeaderCheckBox_Clicked(object sender, EventArgs e) {
                // Your existing code
            }
    
            private void DataGridView_CellClick(object sender, DataGridViewCellEventArgs e) {
                // Your existing code
            }
        }
    }
    

  3. Farshad Valizade 501 Reputation points
    2023-12-07T04:03:01.6266667+00:00

    tnx to all of you but I don't want to add new dg to designer and add it to the form.

    I just want to add a checkboz all to existing dgv because I have used advanced datagrdivew component in my form and I can't remove it and use new dgv.

    0 comments No comments

  4. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-12-07T07:33:32.48+00:00

    See the following article with source code.

    SelectAll_new

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.