DataGridView绑定的CheckBox如何在Sales列输入数值时自动勾选
这个需求可以实现,你可以通过监听DataGridView的单元格编辑结束事件触发自动勾选逻辑,具体实现方案如下:
实现方案(以WinForm场景为例)
你只需要为DataGridView绑定CellEndEdit事件,在事件中判断当前编辑的是否为Sales列,再根据输入值自动给复选框列赋值即可,参考代码如下:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { // 仅处理Sales列的编辑事件 if (dataGridView1.Columns[e.ColumnIndex].Name != "Sales") return; DataGridViewRow targetRow = dataGridView1.Rows[e.RowIndex]; string inputContent = targetRow.Cells["Sales"].Value?.ToString() ?? string.Empty; // 输入不为空则自动勾选复选框,可根据需求调整判断逻辑 // 注意将代码中的IsChecked替换为你自己的复选框列的实际名称 targetRow.Cells["IsChecked"].Value = !string.IsNullOrWhiteSpace(inputContent); }
可选逻辑调整
如果你要求只有输入合法数值时才勾选,可以将上面的赋值逻辑替换为:
// 校验输入是否为合法的数值(可根据需求替换为int、double等类型的校验) targetRow.Cells["IsChecked"].Value = decimal.TryParse(inputContent, out _);
效果参考

内容的提问来源于stack exchange,提问作者Jewelserniabad
相关产品推荐
相关产品推荐

