0

how to create a checkBox header in dataGridView , I have a dataGridView and i have a CheckBox Column index=12 and i need to do a checkBox on the header , this is my code but i have a error how can i do change this code to fix it , to create the checkBox header. any body can help???? i add some code to explain what i need to do.

DataGridViewCheckBoxCell checkBoxCell = new DataGridViewCheckBoxCell();
 //i have error here
dataGridViewSellers.Columns[12].HeaderCell = checkBoxCell;

foreach(Seller aSeller in sellers){
    DataGridViewButtonCell buttonEditCell = new DataGridViewButtonCell();
    DataGridViewButtonCell buttonDeleteCell = new DataGridViewButtonCell();
    DataGridViewButtonCell buttonRestPassCell = new DataGridViewButtonCell();
    dataGridViewSellers[0, i].Value = aSeller.IdNumber;
    dataGridViewSellers[1, i].Value = aSeller.Name;
    dataGridViewSellers[2, i].Value = aSeller.LastName;
    dataGridViewSellers[3, i].Value = aSeller.PhoneNumber;
    dataGridViewSellers[4, i].Value = aSeller.Address;
    dataGridViewSellers[5, i].Value = aSeller.Email;
    dataGridViewSellers[6, i].Value = aSeller.UserName;
    dataGridViewSellers[7, i].Value = aSeller.EntryDate;
    dataGridViewSellers[8, i].Value = aSeller.BusinessHours;
    dataGridViewSellers[9, i] = buttonEditCell;
    dataGridViewSellers[10, i] = buttonDeleteCell;
    dataGridViewSellers[11, i] = buttonRestPassCell;
}
3

1 Answer 1

0

This is the fixed code..

    private void AddHeaderCheckBox()
    {
        HeaderCheckBox = new CheckBox();

        HeaderCheckBox.Size = new Size(15, 15);

        //Add the CheckBox into the DataGridView
        this.dataGridViewSellers.Controls.Add(HeaderCheckBox);
    }

    private void ResetLocation(int ColumnIndex, int RowIndex)
    {
        //Get the column header cell bounds
        Rectangle oRectangle =
          this.dataGridViewSellers.GetCellDisplayRectangle(ColumnIndex, RowIndex, true);

        Point oPoint = new Point();


        oPoint.X = oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox.Width) / 2 + 1;
        oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1;

        //Change the location of the CheckBox to make it stay on the header
        HeaderCheckBox.Location = oPoint;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.