I\'m developing a window application in C# VS2005. I have a dataGridView in which the first column has Checkboxes. Now i want the Column header also to be a CheckBox which i
The above solution is kind of good, but there is also an easier way! Simply add these two methods and then you would have what you want!
First add a show_chkBox
method to your code and call it in the onload
function of your form or after creating your DataGridView
:
private void show_chkBox()
{
Rectangle rect = dataGridView1.GetCellDisplayRectangle(0, -1, true);
// set checkbox header to center of header cell. +1 pixel to position
rect.Y = 3;
rect.X = rect.Location.X + (rect.Width/4);
CheckBox checkboxHeader = new CheckBox();
checkboxHeader.Name = "checkboxHeader";
//datagridview[0, 0].ToolTipText = "sdfsdf";
checkboxHeader.Size = new Size(18, 18);
checkboxHeader.Location = rect.Location;
checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);
dataGridView1.Controls.Add(checkboxHeader);
}
and then you would have the checkbox in the header.
For the selecting problem, just add this code:
private void checkboxHeader_CheckedChanged(object sender, EventArgs e)
{
CheckBox headerBox = ((CheckBox)dataGridView1.Controls.Find("checkboxHeader", true)[0]);
int index = 0;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
dataGridView1.Rows[i].Cells[0].Value = headerBox.Checked;
}
}