C# 验证datagrid C中的复选框#
我有一个未绑定的datagrid,它有两列定义为复选框 我试图完成的是在用户单击“删除”列中出现的任何列时执行验证。在某些情况下,我不希望用户能够删除某些记录 在处理这个问题时,我发现每次单击Delete列中的单元格时,都不会调用CellValidating或CellValueChanged方法 我读过类似的问题,但我还没有找到我想要实现的目标 提前感谢您花时间和精力回答我的问题 var checkBox=dataGridView1.Rows[e.RowIndex]。单元格[e.ColumnIndex]作为DataGridViewCheckBoxCellC# 验证datagrid C中的复选框#,c#,datagrid,C#,Datagrid,我有一个未绑定的datagrid,它有两列定义为复选框 我试图完成的是在用户单击“删除”列中出现的任何列时执行验证。在某些情况下,我不希望用户能够删除某些记录 在处理这个问题时,我发现每次单击Delete列中的单元格时,都不会调用CellValidating或CellValueChanged方法 我读过类似的问题,但我还没有找到我想要实现的目标 提前感谢您花时间和精力回答我的问题 var checkBox=dataGridView1.Rows[e.RowIndex]。单元格[e.ColumnI
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));
}
}
在复选框中为Checked添加一个事件,并在那里应用您的逻辑。
您可以使用
CellContentClick
事件处理DataGridView
中的复选框。我动态创建了DataGridView
对象MyDataGridView
。您也可以使用designer来完成。我不确定您想如何使用复选框的值,但是如果您使用它,您应该在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,新DataGridViewCellEventArgs(2,0))
这里的2
是ColumnIndex
,0
是RowIndex
的魅力所在。谢谢事实证明,我过早地说这是有效的。再次调用CellContentClickgets时,即使checkbox.Value设置为false,也不会取消选中复选框。调用此方法相当于手动选中/取消选中复选框。注意使用的索引。或者给我一个具体的例子,当它不起作用时。