我有一个未绑定的数据网格,其中有两列定义为复选框。
我要完成的工作是,当用户单击出现在删除列中的任何列时,执行验证。在某些情况下,我不希望用户删除某些记录。
在进行此操作时,我发现每次单击“删除”列中的单元格都不会调用CellValidating或CellValueChanged方法。
我已经读过类似的问题,但是我还没有找到我要完成的任务。
预先感谢您的时间和精力来回答我的问题。
var checkBox = dataGridView1.Rows [e.RowIndex] .Cells [e.ColumnIndex]作为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));
}
}
答案 0 :(得分:1)
在复选框中为“已检查”添加事件,然后在其中应用逻辑。 https://www.dotnetperls.com/checkbox-wpf
答案 1 :(得分:1)
您可以使用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);
}
}
}