【问题标题】:Validating a Checkbox in a datagrid C#验证数据网格 C# 中的复选框
【发布时间】:2018-07-12 20:25:58
【问题描述】:

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

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

在处理此问题时,我发现每次单击“删除”列中的单元格时都不会调用 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


    【解决方案1】:

    在复选框中为 Checked 添加一个事件,并在那里应用您的逻辑。 https://www.dotnetperls.com/checkbox-wpf

    【讨论】:

      【解决方案2】:

      您可以使用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);
              }
          }
      }
      

      【讨论】:

      • 感谢您的代码。它似乎在做我想做的事。我唯一的额外问题是我想以编程方式取消选中该复选框。由于这不是一个验证函数,我可以返回一个值,该值会导致 .Net 取消选中该复选框还是我必须自己这样做?
      • @L.Levine 您可以轻松地以编程方式取消选中该复选框。最快的解决方案之一是手动调用事件处理程序。例如,称之为MyDataGridView_CellContentClick(null, new DataGridViewCellEventArgs(2, 0)); 这里2ColumnIndex0RowIndex
      • 工作就像一个魅力。谢谢!
      • 原来我说这行得通还为时过早。再次调用 CellContentClickgets 时,即使 checkBox.Value 设置为 false,复选框也不会被取消选中。
      • 调用这个方法相当于手动选中/取消选中复选框。注意使用的索引。或者当它不起作用时给我具体的例子。
      猜你喜欢
      • 2012-03-11
      • 1970-01-01
      • 2020-03-22
      • 1970-01-01
      • 2020-12-16
      • 2011-01-24
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      相关资源
      最近更新 更多