下面是我的代码,在这个事件中检测状态,我的checkbox列是设计时加入的,运行时这面下的代码没有问题,但是只能慢慢的点,速度一快了,比如双击一下,旁边的审核栏不变化了,而这个状态栏却变了。应该怎么解决这个问题,到底是怎么回事啊。先谢谢大家了。
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
if (e.ColumnIndex == 0)
{
if (((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).Tag == null)
{
((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).Tag = "1";
this.dataGridView1.Rows[e.RowIndex].Cells[8].Value = "已审核";
}
else if (Convert.ToBoolean(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value) == true)
{
((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).Tag = "0";
this.dataGridView1.Rows[e.RowIndex].Cells[8].Value = "";
}
else
{
((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).Tag = "1";
this.dataGridView1.Rows[e.RowIndex].Cells[8].Value = "已审核";
}
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
if (e.ColumnIndex == 0)
{
if (((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).Tag == null)
{
((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).Tag = "1";
this.dataGridView1.Rows[e.RowIndex].Cells[8].Value = "已审核";
}
else if (Convert.ToBoolean(this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value) == true)
{
((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).Tag = "0";
this.dataGridView1.Rows[e.RowIndex].Cells[8].Value = "";
}
else
{
((DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex]).Tag = "1";
this.dataGridView1.Rows[e.RowIndex].Cells[8].Value = "已审核";
}
}
}
{ CheckBox chkExport;
string StrSql = string.Empty;
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
chkExport = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (chkExport.Checked == true)
{
StrSql += "" + ((Label)GridView1.Rows[i].FindControl("lbId")).Text.Trim() + ",";
}
}
}
返回的是 checked 行 的 主键值 组成的 字符串
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
if (e.ColumnIndex == 8)
{
if (this.dataGridView1.Rows[e.RowIndex].Cells[8].FormattedValue.ToString() == "True")
{
this.dataGridView1.Rows[e.RowIndex].Cells[7].Value = "已审核";
}
else
{
this.dataGridView1.Rows[e.RowIndex].Cells[7].Value = "";
}
}
}
现在的问题是该事件是“单击单元格”的事件,只要是单击就没事,如果双击或乱击,就不对了,有时虽然勾选状态变了,但是没执行代码。如果用按空格键的方式代替单击动作,就没问题,严格按照逻辑判断来执行。请问,怎么能够真正依靠该单元格的状态值来执行。
if (cell.Value != null)
{
if ((bool)cell.Value == true)
{
你的动作
}
}
把CellContentClick事件中的代码copy一份就可以了