c# - 验证数据网格 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));
}
}
解决方案
您可以使用该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);
}
}
}
推荐阅读
- ruby-on-rails - Rails - NameError - 未初始化的常量
- google-chrome - 有没有办法将消息附加到网页上的电报链接的 tg:// 处理程序 url?
- css - 对话框中的双滚动条
- android - 让 Kotlin Serializer 与 Retrofit 一起工作
- c - 关于缺少原型错误的说明
- javascript - 我可以使用 Tensorflow.js 进行无监督学习吗?
- javascript - 在 youtube iframe 中隐藏相关视频
- php - 尝试连接到 Azure SQL 服务器时,我得到一个 PDOException 找不到驱动程序
- html - 如何控制 div 和 svg 的位置
- wpf - XAML WPF 中 {x:Static} 和 {StaticResource} 之间的区别