Mehmet Fatih

Mehmet Fatih

  • 855
  • 931
  • 39.9k

Header Checkbox

Jun 5 2023 12:23 PM

I want to insert a header checkbox in the datagridview. How can I do select all checkbox header? I am trying this codes but I couldn't succeed it.

private void PersonelEkle_Load(object sender, EventArgs e)
{
    Point headerCellLocation = this.dataGridView1.GetCellDisplayRectangle(0, -1, true).Location;
    DataGridViewCheckBoxColumn CBColumn = new DataGridViewCheckBoxColumn();
    CBColumn.Name = "X";
    CBColumn.HeaderText = "ColumnHeader";
    CBColumn.Width = 30;
    CBColumn.FalseValue = "0";
    CBColumn.TrueValue = "1";
    dataGridView1.Columns.Insert(0, CBColumn);
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{       
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
        chk.Value = !(chk.Value == null ? false : (bool)chk.Value); //because chk.Value is initialy null
    }
}

 


Answers (3)