I have got a header checkbox for SelectAll in my checkboxColumn of my datagridview
private void AddSelectAllCheckBox(DataGridView theDataGridView)
{
CheckBox cbx = new CheckBox();
cbx.Name = "SelectAll";
//The box size
cbx.Size = new Size(14, 14);
Rectangle rect = default(Rectangle);
rect = theDataGridView.GetCellDisplayRectangle(0, -1, true);
//Put CheckBox in the middle-center of the column header.
cbx.Location = new System.Drawing.Point(rect.Location.X + ((rect.Width - cbx.Width) / 2), rect.Location.Y + ((rect.Height - cbx.Height) / 2));
cbx.BackColor = Color.White;
theDataGridView.Controls.Add(cbx);
//Handle header CheckBox check/uncheck function
cbx.Click += HeaderCheckBox_Click;
//When any CheckBox value in the DataGridViewRows changed,
//check/uncheck the header CheckBox accordingly.
//theDataGridView.CellValueChanged += DataGridView_CellChecked;
//This event handler is necessary to commit new CheckBox cell value right after
//user clicks the CheckBox.
//Without it, CellValueChanged event occurs until the CheckBox cell lose focus
//which means the header CheckBox won't display corresponding checked state instantly when user
//clicks any one of the CheckBoxes.
theDataGridView.CurrentCellDirtyStateChanged += DataGridView_CurrentCellDirtyStateChanged;
}
private void HeaderCheckBox_Click(object sender, EventArgs e)
{
this._IsSelectAllChecked = true;
CheckBox cbx = default(CheckBox);
cbx = (CheckBox)sender;
foreach (DataGridViewRow row in metroGrid1.Rows)
{
row.Cells[0].Value = cbx.Checked;
}
metroGrid1.EndEdit();
this._IsSelectAllChecked = false;
}
//The CurrentCellDirtyStateChanged event happens after user change the cell value,
//before the cell lose focus and CellValueChanged event.
private void DataGridView_CurrentCellDirtyStateChanged(System.Object sender, System.EventArgs e)
{
DataGridView dataGridView = (DataGridView)sender;
if (dataGridView.CurrentCell is DataGridViewCheckBoxCell)
{
//When the value changed cell is DataGridViewCheckBoxCell, commit the change
//to invoke the CellValueChanged event.
dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
This is working fine.
If I check or uncheck the Checkbox in its row this event is fired and works:
private void metroGrid1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex > -1)
{
//listBox1.Items.Add(" -> ");
DataGridView dgv = sender as DataGridView;
if (dgv == null)
return;
//if (dgv.CurrentRow)
//{
if (Convert.ToBoolean(dgv.CurrentRow.Cells[Sel.Name].Value) == true)
{
int selCount = Int32.Parse(metroLabel5.Text);
int addCount = selCount + Int32.Parse(dgv.CurrentRow.Cells[Jobs.Name].Value.ToString());
metroLabel5.Text = addCount.ToString();
//listBox1.Items.Add(dgv.CurrentRow.Index.ToString() + " -> " + dgv.CurrentRow.Cells[Jobs.Name].Value.ToString());
}
if (Convert.ToBoolean(dgv.CurrentRow.Cells[Sel.Name].Value) == false)
{
int selCount = Int32.Parse(metroLabel5.Text);
int minCount = selCount - Int32.Parse(dgv.CurrentRow.Cells[Jobs.Name].Value.ToString());
metroLabel5.Text = minCount.ToString();
// listBox1.Items.Add(dgv.CurrentRow.Index.ToString() + " -> " + dgv.CurrentRow.Cells[Jobs.Name].Value.ToString());
}
//}
}
}
private void metroGrid1_OnCellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex > -1)
{
metroGrid1.EndEdit();
}
}
My problem: If I check or uncheck the headerCheckbox the CellValueChanged Event is fired for each row which is ok, but it always uses the first row for the event.
For Example: I have 8 rows with jobs 1,2,3,4,5,6,7,8 so the metrolabel1.text should be 36 if all are selected, but with the selectAll checkbox it goes to 8 (8 time row 0 with 1 jobs)
Aucun commentaire:
Enregistrer un commentaire