温文尔雅的甜瓜 · 删除数据 | PingCAP 文档中心· 1 年前 · |
爱运动的手电筒 · 视频数据处理方法!关于开源软件FFmpeg视 ...· 1 年前 · |
大方的蚂蚁 · Jquery选择器中使用变量实现动态选择例子 ...· 1 年前 · |
慷慨的包子 · 【译】使用SQL生成非均匀随机数 - ...· 1 年前 · |
面冷心慈的枕头 · linux grep命令详解-天翼云· 1 年前 · |
有人能帮我解释一下为什么它不工作吗?我有一个
checkbox
,如果我点击它,应该会取消选中datagridview中的所有复选框,这些复选框是在包含user selected复选框之前选中的。
代码如下:
private void chkItems_CheckedChanged(object sender, EventArgs e)
foreach (DataGridViewRow row in datagridview1.Rows)
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1];
if (chk.Selected == true)
chk.Selected = false;
chk.Selected = true;
}
不应选中该复选框。应该检查它。
下面是添加的列
DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
CheckBox chk = new CheckBox();
CheckboxColumn.Width = 20;
datagridview1.Columns.Add(CheckboxColumn);
发布于 2012-11-12 14:15:04
看看这个 MSDN Forum Posting ,它建议将单元格值与 Cell.TrueValue 进行比较。
所以按照它的例子,你的代码应该是这样的:(这是完全未经测试的)
编辑:似乎未绑定DataGridViewCheckBox的Cell.TrueValue的默认值是null,您需要在列定义中设置它。
private void chkItems_CheckedChanged(object sender, EventArgs e)
foreach (DataGridViewRow row in dataGridView1.Rows)
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[1];
if (chk.Value == chk.TrueValue)
chk.Value = chk.FalseValue;
chk.Value = chk.TrueValue;
}
下面的代码是在构造函数中设置TrueValue和FalseValue以及检查是否为空的工作说明:
public partial class Form1 : Form
public Form1()
InitializeComponent();
DataGridViewCheckBoxColumn CheckboxColumn = new DataGridViewCheckBoxColumn();
CheckboxColumn.TrueValue = true;
CheckboxColumn.FalseValue = false;
CheckboxColumn.Width = 100;
dataGridView1.Columns.Add(CheckboxColumn);
dataGridView1.Rows.Add(4);
private void chkItems_CheckedChanged(object sender, EventArgs e)
foreach (DataGridViewRow row in dataGridView1.Rows)
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
if (chk.Value == chk.FalseValue || chk.Value == null)
chk.Value = chk.TrueValue;
chk.Value = chk.FalseValue;
dataGridView1.EndEdit();
}
发布于 2014-08-03 08:18:39
当我看到这篇文章没有得到回复时,我正在制作我自己版本的复选框来控制一个DataGridViewCheckBoxColumn。要设置DataGridViewCheckBoxCell的选中状态,请执行以下操作:
foreach (DataGridViewRow row in dataGridView1.Rows)
dataGridView1.Rows[row.Index].SetValues(true);
}
对于任何其他想要完成同样的事情的人来说,这是我想出来的。
这使得这两个控件的行为类似于Gmail中的复选框列。它保留了鼠标和键盘的功能。
using System;
using System.Windows.Forms;
namespace Check_UnCheck_All
public partial class Check_UnCheck_All : Form
public Check_UnCheck_All()
InitializeComponent();
dataGridView1.RowCount = 10;
dataGridView1.AllowUserToAddRows = false;
this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvApps_CellContentClick);
this.dataGridView1.CellMouseUp += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.myDataGrid_OnCellMouseUp);
this.dataGridView1.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.myDataGrid_OnCellValueChanged);
this.checkBox1.Click += new System.EventHandler(this.checkBox1_Click);
public int chkInt = 0;
public bool chked = false;
public void myDataGrid_OnCellValueChanged(object sender, DataGridViewCellEventArgs e)
if (e.ColumnIndex == dataGridView1.Rows[0].Index && e.RowIndex != -1)
DataGridViewCheckBoxCell chk = dataGridView1.Rows[e.RowIndex].Cells[0] as DataGridViewCheckBoxCell;
if (Convert.ToBoolean(chk.Value) == true) chkInt++;
if (Convert.ToBoolean(chk.Value) == false) chkInt--;
if (chkInt < dataGridView1.Rows.Count && chkInt > 0)
checkBox1.CheckState = CheckState.Indeterminate;
chked = true;
else if (chkInt == 0)
checkBox1.CheckState = CheckState.Unchecked;
chked = false;
else if (chkInt == dataGridView1.Rows.Count)
checkBox1.CheckState = CheckState.Checked;
chked = true;
public void myDataGrid_OnCellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
// End of edition on each click on column of checkbox
if (e.ColumnIndex == dataGridView1.Rows[0].Index && e.RowIndex != -1)
dataGridView1.EndEdit();
dataGridView1.BeginEdit(true);
public void dgvApps_CellContentClick(object sender, DataGridViewCellEventArgs e)
if (dataGridView1.CurrentCell.GetType() == typeof(DataGridViewCheckBoxCell))
if (dataGridView1.CurrentCell.IsInEditMode)
if (dataGridView1.IsCurrentCellDirty)
dataGridView1.EndEdit();
dataGridView1.BeginEdit(true);
public void checkBox1_Click(object sender, EventArgs e)
if (chked == true)
foreach (DataGridViewRow row in dataGridView1.Rows)
DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];
if (chk.Value == chk.TrueValue)
chk.Value = chk.FalseValue;
chk.Value = chk.TrueValue;
chked = false;
chkInt = 0;
return;
if (chked == false)
foreach (DataGridViewRow row in dataGridView1.Rows)
dataGridView1.Rows[row.Index].SetValues(true);
chked = true;
chkInt = dataGridView1.Rows.Count;
}
发布于 2016-04-22 20:14:26
对你来说很简单的代码它会工作的
private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
if (dgv.CurrentRow.Cells["ColumnNumber"].Value != null && (bool)dgv.CurrentRow.Cells["ColumnNumber"].Value)
dgv.CurrentRow.Cells["ColumnNumber"].Value = false;
dgv.CurrentRow.Cells["ColumnNumber"].Value = null;