jeudi 25 novembre 2021

adding checkbox header in DGV c#

I saw a code to add a checkbox header in a DGV, but it is not working fine with my DGV.

the code:

private void addCheckBoxHeader()
        {
            DataGridViewCheckBoxColumn checkboxColumn = new DataGridViewCheckBoxColumn();
            checkboxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            checkboxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            DGViewListItems.Columns.Insert(0, checkboxColumn);

            Rectangle rect = DGViewListItems.GetCellDisplayRectangle(0, -1, false);
            rect.X = rect.Location.X + (rect.Width / 2);
            rect.Y = rect.Location.X + (rect.Width / 2);
            checkboxHeader = new CheckBox();
            checkboxHeader.BackColor = Color.FromArgb(0, 80, 200);
            checkboxHeader.Name = "checkboxHeader";
            checkboxHeader.Location = rect.Location;

            //checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);

            DGViewListItems.Controls.Add(checkboxHeader);
            
        }

This is the output of the code above:

enter image description here

What is wrong with the code? How can I put the checkbox header correctly?

also, nevermind the Column1 column that is the column i added in the Design




Aucun commentaire:

Enregistrer un commentaire