首页 > 解决方案 > 验证数据网格 C# 中的复选框

问题描述

我有一个未绑定的数据网格,其中有两列定义为复选框。

我要完成的是当用户单击删除列中出现的任何列时执行验证。在某些情况下,我不希望用户能够删除某些记录。

在处理此问题时,我发现每次单击“删除”列中的单元格时都不会调用 CellValidating 或 CellValueChanged 方法。

我读过类似的问题,但我还没有确切地找到我想要完成的事情。

提前感谢您为回答我的问题所付出的时间和精力。

在此处输入图像描述

var checkBox = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;

            var isCheck = checkBox?.Value;
            var check = isCheck == null ? false : bool.Parse(isCheck.ToString());

            if (isCheck != null)
                checkBox.Value = !check;        // change checkbox value

            if (!check) // if value was false, it's being changed to true
            {
                string sShipToId = dataGridView1.Rows[e.RowIndex].Cells[(int)ColumnHeaders.ShipToIDColumn].Value.ToString();
                string sDelete = dataGridView1.Rows[e.RowIndex].Cells[(int)ColumnHeaders.DeleteColumn].Value.ToString();


                // need to check to see if this ship to is associated with an open order. if it is
                // we can't delete it. This mimics the functionality that P21 exhibits when you try to delete
                // a ship to from the Ship To Maintenance screen.
                // we also need to check and see if we're deleting the Ship to associated with the  current order.

                ShipToInfo Ship = new ShipToInfo(Server, Database);

                if ((string.IsNullOrEmpty(sShipTo) == false) &&
                        (sShipToId.Equals(sShipTo) == true) ||
                        (Ship.ShipToExistsInOpenOrder(sShipToId, CompanyID) == true))
                {
                    MessageBox.Show("Open orders exist for this Ship To " + sShipToId + ". It cannot be deleted.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    dataGridView1_CellContentClick(null, new DataGridViewCellEventArgs(e.ColumnIndex, e.RowIndex));
                }
            }

标签: c#datagrid

解决方案


您可以使用该CellContentClick事件来处理DataGridView. 我动态创建了DataGridView对象MyDataGridView。您也可以使用设计器来完成。我不确定你想如何使用它的值,CheckBox但如果你使用它,你应该手动更改它的值,CellContentClick如下所示。

private DataGridView MyDataGridView = new DataGridView();

public Form1()
{
    InitializeComponent();
    SetupDataGridView();
}

private void SetupDataGridView()
{
    this.Controls.Add(MyDataGridView);
    MyDataGridView.ColumnCount = 2;

    MyDataGridView.Name = "MyDataGridView";
    MyDataGridView.Location = new Point(10, 10);
    MyDataGridView.Size = new Size(500, 300);

    MyDataGridView.Columns[0].Name = "ID";
    MyDataGridView.Columns[1].Name = "Value";

    MyDataGridView.Columns.Add(new DataGridViewCheckBoxColumn { Name = "Default" });
    MyDataGridView.Columns.Add(new DataGridViewCheckBoxColumn { Name = "Delete" });

    MyDataGridView.Rows.Add("1", "one", true, true);
    MyDataGridView.Rows.Add("2", "two", false, true);
    MyDataGridView.Rows.Add("3", "three", true, false);

    MyDataGridView.CellContentClick += MyDataGridView_CellContentClick;
}

private void MyDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    // get value of checkbox
    var checkBox = MyDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
    var isCheck = checkBox?.Value;
    var check = isCheck == null ? false : bool.Parse(isCheck.ToString());

    if (isCheck != null)            
        checkBox.Value = !check;        // change checkbox value

    if (e.ColumnIndex == 3 && check)
    {
        DialogResult dialogResult = MessageBox.Show("Are you Sure", 
            "Delete Row", MessageBoxButtons.YesNo);
        if (dialogResult == DialogResult.Yes)
        {
            MyDataGridView.Rows.RemoveAt(e.RowIndex);
        }
    }
}

推荐阅读