如何強制在Datagridview的列中僅檢查單個複選框?Datagridview只強制在列中選中一個複選框
回答
您必須訂閱網格的CellValueChanged
事件,並根據當前單元格的檢查狀態,循環DataGridView並將true/false設置爲其他單元格的值。
void grd_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
{
if (Convert.ToBoolean(((sender as DataGridView).CurrentCell as DataGridViewCheckBoxCell).Value))
{
// Maybe have a method which does the
//loop and set value except for the current cell
}
}
}
一個客戶端解決方案(jQuery的可以幫助很多)是更加可取的,這將避免幾十回發 – 2012-07-13 08:19:45
@SteveB檢查標記其** winform ** – V4Vendetta 2012-07-13 08:29:20
@ V4Vendetta只是一個問題,爲什麼CellValueChanged而不是CellContentClick?據我所知,CellValueChanged事件是當任何單元格的內容改變時拋出,但CellContentClick事件拋出時,單元格中的某些內容已被點擊(IE:在這種情況下的複選框),所以不會使用CellContentClick事件比較好? – l46kok 2012-07-13 08:36:43
可以使用CellEndEdit
事件DGV
,因爲當細胞被修改後,會出現。請閱讀下面的代碼片段中的評論,以使用下面的代碼:
private void dgrvUserProfileView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
int CheckedCount = 0;
//Make sure you have set True Value/ false Value property of check box column to 1/0 or true/false resp.
//Lets say your column 5th(namely Department) is a checked box column
if (dgrvUserProfileView.Columns[e.ColumnIndex].Name == "Department")
{
for (int i = 0; i <= dgrvUserProfileView.Rows.Count - 1; i++)
{
if (Convert.ToBoolean(dgrvUserProfileView.Rows[i].Cells["Department"].Value) == true)
{
CheckedCount = CheckedCount + 1;
}
}
if (CheckedCount == 1)
{
for (int i = 0; i <= dgrvUserProfileView.Rows.Count - 1; i++)
{
if (Convert.ToBoolean(dgrvUserProfileView.Rows[i].Cells["Department"].Value) == true)
{
dgrvUserProfileView.Rows[i].Cells["Department"].ReadOnly = true;
}
}
}
else
{
for (int i = 0; i <= dgrvUserProfileView.Rows.Count - 1; i++)
{
dgrvUserProfileView.Rows[i].Cells["Department"].ReadOnly = false;
}
}
}
}
希望這有助於!
或從這個答案採取參考http://stackoverflow.com/questions/10438613/datagridview-how-can-i-make-a-checkbox-act-as-a-radio-button – akhil 2012-07-13 08:12:13
private void dgvlist_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int currentcolumnclicked = e.ColumnIndex;
for (int i = 0; i <= dgvlist.Columns.Count - 1; i++)
{
if (dgvlist.Columns[i] is DataGridViewCheckBoxColumn)
{
if (Convert.ToString(dgvlist.CurrentRow.Cells[i].EditedFormattedValue) == "True" && i !=currentcolumnclicked)
{
dgvlist.CurrentRow.Cells[i].Value = false;
}
}
}
}
private void dataGridViewProduit_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
{
if (Convert.ToBoolean(((sender as DataGridView).CurrentCell as DataGridViewCheckBoxCell).Value))
{
foreach (DataGridViewRow row in (sender as DataGridView).Rows)
{
if (row.Index != (sender as DataGridView).CurrentCell.RowIndex && Convert.ToBoolean(row.Cells[e.ColumnIndex].Value) == true)
{
row.Cells[e.ColumnIndex].Value = false;
}
}
}
}
}
private void dataGridViewClient_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (this.dataGridViewClient.IsCurrentCellDirty)
{
dataGridViewClient.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 1)
{
try
{
string val = dataGridView3.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
if (val == "False")
val = "True";
else if (val == "True")
val = "False";
dataGridView3.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = val;
for (int i = 0; i < dataGridView3.Rows.Count; i++)
{
string active = "";
if (i != e.RowIndex)
{
if (val == "False")
{
dataGridView3.Rows[i].Cells[1].Value = "True";
active = "Y";
}
else if (val == "True")
{
dataGridView3.Rows[i].Cells[1].Value = "False";
active = "N";
}
}
else
{
if (val == "False")
active = "N";
else
active = "Y";
}
}
}
catch (Exception ex)
{ }
}
}
private void dgvCaixa_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
{
foreach (DataGridViewRow row in dgvCaixa.Rows)
{
if (row.Index != dgvCaixa.CurrentCell.RowIndex && Convert.ToBoolean(row.Cells[e.ColumnIndex].Value) == true)
{
row.Cells[e.ColumnIndex].Value = false;
}
}
}
}
你能解釋這是如何解決問題。 – 2014-02-15 12:33:43
private void grdRegClass_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (grdRegClass.Columns.IndexOf(grdRegClass.Columns["Status"]) == e.ColumnIndex)
{
int currentcolumnclicked = e.ColumnIndex;
int currentrowclicked = e.RowIndex;
foreach (DataGridViewRow dr in grdRegClass.Rows)
{
dr.Cells[currentcolumnclicked].Value = false;
}
grdRegClass.CurrentRow.Cells[currentrowclicked].Value = true;
}
}
在vb.net:
Private Sub DataGridViewJobsList_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewJobsList.CellValueChanged
If TypeOf TryCast(sender, DataGridView).CurrentCell Is DataGridViewCheckBoxCell Then
Dim cell1 As DataGridViewCell
cell1 = TryCast(sender, DataGridView).CurrentCell
If cell1.Value = True Then
For Each row As DataGridViewRow In TryCast(sender, DataGridView).Rows
If row.Index <> cell1.RowIndex AndAlso row.Cells(e.ColumnIndex).Value = "1" Then
row.Cells(e.ColumnIndex).Value = False
End If
Next
End If
End If
End Sub
Private Sub DataGridViewJobsList_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridViewJobsList.CurrentCellDirtyStateChanged
If Me.DataGridViewJobsList.IsCurrentCellDirty Then
DataGridViewJobsList.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
您可以將VirtualMode設置設置爲TRUE 上的DGV只允許一個複選框。並且你可以設置VirtualMode設置爲假對DatagridView檢查多個。
我認爲這是解決方案最簡單的方法。
- 1. Datagridview複選框列只允許我選擇一個複選框
- 2. 從DataGridView中的多行復選框中只選擇一個複選框
- 3. 只允許在DataGridView中選擇一個複選框
- 4. datagridView中的複選框列
- 5. 強制動態生成的複選框只允許選擇一個複選框
- 6. Datagridview複選框列有一個死區
- 7. 強制一個或多個複選框被選中
- 8. 只有一個複選框被選中
- 9. 強制複選框被選中
- 10. 只選一個複選框
- 11. 只有一個複選框在複選框列表檢查
- 12. HTML複選框 - 只允許選中一個複選框
- 13. 選中一個複選框,選中另一個複選框
- 14. 在每個複選框/下拉列表中強制執行POST
- 15. 製作一個複選框選中
- 16. 只顯示在DataGridView中選中的框
- 17. datagridview複選框列排序
- 18. Datagridview複選框列的值
- 19. Datagridview複選框列驗證
- 20. Datagridview和複選框列表
- 21. Powershell DataGridView - 在複選框列中循環查看是否選中
- 22. 如何檢查在datagridview列中是否選中複選框
- 23. 將複選框列添加到DataGridView中
- 24. 複選框datagridview
- 25. dataGridView中的複選框
- 26. 從2列複選框中選擇一個複選框
- 27. 從複選框列表中選擇一個複選框
- 28. 限制 - 只檢查一個複選框
- 29. 如何在N級列表視圖中一次只選中一個複選框
- 30. 複選框::一次只能選一個
爲了與整個計算機軟件行業保持一致,請使用單選按鈕而不是複選框。列表複選框意味着您可以選擇多個項目,而單選按鈕意味着您只能爲該組選擇一個項目。可以解決您的問題,但我強烈建議您反對這個方向。如果您選擇複選框而不是單選按鈕,因爲您不允許添加值,請添加單選按鈕「無」或「清除」按鈕以選擇無選項。 – 2012-07-13 08:09:46
@SteveB我知道我遲到這個評論,但我沒有看到Winforms Datagridview中的單選按鈕? – 2013-02-12 13:04:41