根据某些条件更改复选框列中的复选框状态

时间:2009-08-07 10:55:24

标签: c# .net

我有一个datagridview,其数据源是一个数据表。 我正在为此datagridview添加一个复选框列。 我遇到的两个问题: 1.当用户点击该列中的任何单元格时,我首先需要获取该复选框的状态。 2.如果未选中状态,则会弹出一个消息框,询问用户是否要检查它。 这是YesNoCancel消息框。如果用户选择“是”或“否”,则复选框的状态应转为检查。对于取消,应将其保留为原始状态。 如果检查状态并且用户单击该复选框,则会弹出一个消息框,询问用户是否要取消选中它(YesNo),是,它应该将状态更改为未选中状态,然后选择“否”,将该复选框保留为原始状态

以下代码似乎不起作用:

if (e.RowIndex != -1)
        {

            DataGridViewRow dr = dataGridView1.CurrentRow;

            DataGridViewCheckBoxCell chkCell = (DataGridViewCheckBoxCell)dr.Cells[0];

            if (chkCell.Value == null)
            {
                DialogResult dResult = MessageBox.Show("Do you want to mark this value for deletion?","Delete",MessageBoxButtons.YesNoCancel);
                if (dResult == DialogResult.Yes)
                {
                    chkCell.Value = true;

                }
                else if (dResult == DialogResult.No)
                {
                    chkCell.Value = true;

                }
                else if (dResult == DialogResult.Cancel)
                {
                    chkCell.Value = false;

                }
            }

            //else if ((bool)chkCell.Value == true)
            else if ((bool)chkCell.Value == true)
            {
               DialogResult dResult = MessageBox.Show("Do you want to unmark this value!","Delete",MessageBoxButtons.YesNo);
               if (dResult==DialogResult.Yes)
                {
                    chkCell.Value = false;
                }
               else
               {
                   chkCell.Value = true;
               }
            }
            else if ((bool)chkCell.Value == false)
            {
                DialogResult dResult = MessageBox.Show("Do you want to mark this value for deletion?", "Delete", MessageBoxButtons.YesNoCancel);
                if (dResult == DialogResult.Yes)
                {
                    chkCell.Value = true;
                }
                else if (dResult == DialogResult.No)
                {
                    chkCell.Value = false;
                }

            }
        }

代码位于CellContentClick事件中。

1 个答案:

答案 0 :(得分:1)

管理null案例的异常,我认为它更好。

然后我用一些附加内容重写了你的代码,即datagridview的EndEdit方法,在更改回以前的值之前提交原始行修改。

if (e.RowIndex != -1)
{
    DataGridViewRow dr = dataGridView1.CurrentRow;

    DataGridViewCheckBoxCell chkCell = (DataGridViewCheckBoxCell)dr.Cells[3];

    try 
    {
        if ((bool)chkCell.Value == true)
        {
            DialogResult dResult = MessageBox.Show("Do you want to unmark this value!", "Delete", MessageBoxButtons.YesNo);
            if (dResult == DialogResult.Yes)
            {
                chkCell.Value = false;
            }
            else
            {
                dataGridView1.EndEdit();
                chkCell.Value = true;
            }
        }
        else if ((bool)chkCell.Value == false)
        {
            DialogResult dResult = MessageBox.Show("Do you want to mark this value for deletion?", "Delete", MessageBoxButtons.YesNo);
            if (dResult == DialogResult.Yes)
            {
                chkCell.Value = true;
            }
                else if (dResult == DialogResult.No)
            {
                dataGridView1.EndEdit();
                chkCell.Value = false;
            }
        }
    }
    catch (Exception)
    {
        DialogResult dResult = MessageBox.Show("Do you want to mark this value for deletion?", "Delete", MessageBoxButtons.YesNoCancel);
        if (dResult == DialogResult.Yes)
        {
            chkCell.Value = true;
        }
        else if (dResult == DialogResult.No)
        {
            chkCell.Value = true;
        }
        else if (dResult == DialogResult.Cancel)
        {
            dataGridView1.EndEdit();
            chkCell.Value = false;
        }
    }
}
dataGridView1.EndEdit();
相关问题