我的datagridview的checkboxColumn中有SelectAll的标题复选框
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);
}
}
这很好。
如果我检查或取消选中其行中的复选框,则会触发此事件并起作用:
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();
}
}
我的问题: 如果我选中或取消选中headerCheckbox,则会为每一行触发CellValueChanged事件,但它总是使用该事件的第一行。
例如:我有8行作业1,2,3,4,5,6,7,8所以如果选择了所有,那么metrolabel1.text应该是36,但是使用selectAll复选框它会转到8( 8次排0,有1个工作)
答案 0 :(得分:1)
你应该使用Convert.ToBoolean(dgv.Rows[e.RowIndex].Cells[Sel.Name].Value)
和Int32.Parse(dgv.Rows[e.RowIndex].Cells[Jobs.Name].Value.ToString())
事件将触发属于不同行的每个单元格,因此当您想从该行获取值时,应该引用已触发事件的行:
dgv.Rows[e.RowIndex].Cells["Column Name"].Value
现在,在您的代码中,您将获得dgv.CurrentRow的值。这不是你需要的。