【发布时间】: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));
}
}
【问题讨论】: