繁体   English   中英

如何取消勾选复选框的值

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM