[ad_1]
在此程序中,我希望在选中 datagridview 中的所有复选框后启用该按钮。 目前的情况是,仅当单击列中的最后一个复选框时,该按钮才会启用。 有什么建议可以解决这个问题吗?
我尝试过的:
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e) { var senderGrid = (DataGridView)sender; senderGrid.EndEdit(); for (int i = 0; i <= dataGridView2.Rows.Count - 1; i++) { if (e.RowIndex >= 0) { var cbxCell = (DataGridViewCheckBoxCell)senderGrid.Rows[i].Cells[1]; if ((bool)cbxCell.Value) { btn1.Enabled = true; } else { btn1.Enabled = false; } } } }
解决方案1
private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e) { bool flag = true; var senderGrid = (DataGridView)sender; senderGrid.EndEdit(); for (int i = 0; i <= dataGridView2.Rows.Count - 1; i++) { if (e.RowIndex >= 0) { var cbxCell = (DataGridViewCheckBoxCell)senderGrid.Rows[i].Cells[1]; if (!(bool)cbxCell.Value) { flag = false; } } } btn1.Enabled = flag; }
解决方案2
private void dgvSelectedServices_CellContentClick(object sender, DataGridViewCellEventArgs e) { bool flag = true; var senderGrid = (DataGridView)sender; senderGrid.EndEdit(); for (int i = 0; i <= dgvSelectedServices.Rows.Count - 1; i++) { foreach (DataGridViewRow row in dgvSelectedServices.Rows) { if (e.RowIndex >= 0) { bool isselect = Convert.ToBoolean(row.Cells[2].Value); if (isselect) { if (dgvSelectedServices.SelectedRows.Count == dgvSelectedServices.Rows.Count) { flag = true; } } else { flag = false; } btnNextToWIP.Enabled = flag; } } } }
[ad_2]
コメント