简体   繁体   中英

How to untick check box value

I'm using WinForms Data Grid . There is a column for checkboxes. When the user checks once and try to not checking again there will be a message box is asking,

Already exists! \\nDo you want to change?

. When user click YES previous checkbox will uncheck the and new one will be checked. But if user click NO both will be checked. I want to uncheck new one when user click NO .

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;
                    }
                }
            }
        }

I don't know what's the rest of your code, so I consider that code until your MessageBox works properly as it should. You were not that far, you have to use a string and instead of a boolean :

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";
                }
            }
        }
    }

As I said before, I don't really know how works your codes elsewhere of this part, so you may have to arrange it a bit to fit to your needs.

you may want to try something like this, first declare the checkbox cell in a variable, then set the value to null

        foreach (DataGridViewRow row in UrDGV.Rows)
        {
            DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells["ChkBoxCol"];

            if (ischecked == true)
            {
                chk.Value = null;
            }


        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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