So I have created a checkbox to effectively handle a select all function in a column of checkboxes for a datagridview in C#. Here is the code for that :
private void LoadColumnHeaders_PurchasedFunds()
{
int i = dgvPurchasedFunds.Columns[0].Width;
Rectangle rect = dgvPurchasedFunds.GetCellDisplayRectangle(0, -1, true);
// set checkbox header to center of header cell. +1 pixel to position
rect.Y = 4;
rect.X = i/3;
CheckBox checkboxHeader = new CheckBox();
checkboxHeader.Name = "checkboxHeader";
//datagridview[0, 0].ToolTipText = "sdfsdf";
checkboxHeader.Size = new Size(14, 14);
checkboxHeader.Location = rect.Location;
checkboxHeader.CheckedChanged += new EventHandler(checkboxHeader_CheckedChanged);
dgvPurchasedFunds.Controls.Add(checkboxHeader);
}
private void checkboxHeader_CheckedChanged(object sender, EventArgs e)
{
CheckBox headerBox = ((CheckBox)dgvPurchasedFunds.Controls.Find("checkboxHeader", true)[0]);
for (int i = 0; i < dgvPurchasedFunds.RowCount; i++)
{
dgvPurchasedFunds.Rows[i].Cells[0].Value = headerBox.Checked;
}
}
So this code seems to do what I need it to, and placing the checkbox inside the dgv where I need it to be. The problem I'm having is, if I click on a checkbox cell and then click on the select all checkbox, it doesn't update the aforementioned checkbox cell. Instead it updates everything else.
I figured it's because the dgvPurchasedFunds.CurrentCell is set to the checkbox I'm trying to programatically edit and due to that it's not letting me edit it, but I'm not sure where to reset that in order to allow the event to happen (checkmark the cell), and deselect the current cell without causing any issues.
Any help is appreciated.
Aucun commentaire:
Enregistrer un commentaire