[英]How to untick check box value
我正在使用WinForms數據網格 。 有一個復選框欄。 當用戶檢查一次並嘗試不再次檢查時,將出現一個消息框,
已經存在! \\ n您要更改嗎?
。 當用戶單擊“是”時,上一個復選框將取消選中,然后將選中新的復選框。 但是,如果用戶單擊“ 否” ,則將兩者都選中。 當用戶單擊“ 否”時,我想取消選中新的 。
private void dgTeam1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int pIndex = _list1.playerList.FindIndex(p => p.captain == true);
if (e.ColumnIndex == 6)
{
if (pIndex != -1)
{
DialogResult result = MessageBox.Show("Captain already exists! \nDo you want change?", "Change Captain Confirmation", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
dgTeam1[6, pIndex].Value = false;
else
{
dgTeam1[6, e.RowIndex].Value = false;
}
}
}
}
我不知道剩下的代碼是什么,因此我會考慮該代碼,直到您的MessageBox正常工作為止。 您還不算太遠,必須使用字符串而不是布爾值:
private void dgTeam1_CellClick(object sender, DataGridViewCellEventArgs e)
{
int pIndex = _list1.playerList.FindIndex(p => p.captain == true);
if (e.ColumnIndex == 6)
{
if (pIndex != -1)
{
DialogResult result = MessageBox.Show("Captain already exists! \nDo you want change?", "Change Captain Confirmation", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
dgTeam1[6, pIndex].Value = "false";
}
else
{
dgTeam1[6, e.RowIndex].Value = "false";
}
}
}
}
就像我之前說過的,我真的不知道您的代碼在本部分其他地方的工作方式,因此您可能需要對其進行一些調整以滿足您的需求。
您可能想要嘗試這樣的操作,首先在變量中聲明復選框單元格,然后將值設置為null
foreach (DataGridViewRow row in UrDGV.Rows)
{
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells["ChkBoxCol"];
if (ischecked == true)
{
chk.Value = null;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.