在datagridview中添加了一列DataGridViewCheckBoxColumn,给其赋值为true,dataGridView1.Rows[i].Cells[0].Value = true,可是有时并没有出现小勾(多行的checkbox,有一个被单击过,再来遍历时,这个曾被单击过的checkbox就不会正确的显示或取消小勾),可能是相当于checkbox的Checked属性没有同时赋值成功。
DataGridViewCheckBoxColumn是否也有个类似checked的属性呢?或者怎么保证为其赋值时,能相应的出现或取消小勾?
DataGridViewCheckBoxColumn是否也有个类似checked的属性呢?或者怎么保证为其赋值时,能相应的出现或取消小勾?
private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (this .dataGridView1.IsCurrentCellDirty)
{
this .dataGridView1.CommitEdit(DataGridViewDataErrorContexts .Commit);
}
}
加这个事件试试
但由于第一行始终是选择状态(蓝色选中状态,但是checkbox并没有勾上),所以对第一行无效。即使给其赋值为true,也没有出现勾,怎么办?private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex == -1)//单击表头
{
bool checkAll = false;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value == null)//没有给checkBox列激活即选中的情况下,直接获取值.ToString会报错
{
checkAll = false;//checkBox列还未激活,即都还为选中
break;
}
else if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "True")
{
string a = dataGridView1.Rows[i].Cells[0].Value.ToString();
checkAll = true;
}
else if (dataGridView1.Rows[i].Cells[0].Value.ToString() != "True")
{
checkAll = false;//有未被选中的记录,则标记全选为false,跳出循环
break;
} }
if (checkAll == false) //存在未选的记录,则更新为全选
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[0].Value = true;
}
}
else //如果原为全选状态,则更新为全不选
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[0].Value = false;
}
//下面的代码都无法彻底清空行选中状态(不是勾上)
dataGridView1.ClearSelection();
dataGridView1.CurrentCell = null;
// dataGridView1.Rows[0].Selected = false;
}
}
}
但由于第一行始终是选择状态(蓝色选中状态,但是checkbox并没有勾上),所以对第一行无效。即使给其赋值为true,也没有出现勾,怎么办?private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex == -1)//单击表头
{
bool checkAll = false;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value == null)//没有给checkBox列激活即选中的情况下,直接获取值.ToString会报错
{
checkAll = false;//checkBox列还未激活,即都还为选中
break;
}
else if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "True")
{
string a = dataGridView1.Rows[i].Cells[0].Value.ToString();
checkAll = true;
}
else if (dataGridView1.Rows[i].Cells[0].Value.ToString() != "True")
{
checkAll = false;//有未被选中的记录,则标记全选为false,跳出循环
break;
} }
if (checkAll == false) //存在未选的记录,则更新为全选
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[0].Value = true;
}
}
else //如果原为全选状态,则更新为全不选
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[0].Value = false;
}
//下面的代码都无法彻底清空行选中状态(不是勾上)
dataGridView1.ClearSelection();
dataGridView1.CurrentCell = null;
// dataGridView1.Rows[0].Selected = false;
}
}
}
但由于第一行始终是选择状态(蓝色选中状态,但是checkbox并没有勾上),所以对第一行无效。即使给其赋值为true,也没有出现勾,怎么办?private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex == -1)//单击表头
{
bool checkAll = false;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[0].Value == null)//没有给checkBox列激活即选中的情况下,直接获取值.ToString会报错
{
checkAll = false;//checkBox列还未激活,即都还为选中
break;
}
else if (dataGridView1.Rows[i].Cells[0].Value.ToString() == "True")
{
string a = dataGridView1.Rows[i].Cells[0].Value.ToString();
checkAll = true;
}
else if (dataGridView1.Rows[i].Cells[0].Value.ToString() != "True")
{
checkAll = false;//有未被选中的记录,则标记全选为false,跳出循环
break;
} }
if (checkAll == false) //存在未选的记录,则更新为全选
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[0].Value = true;
}
}
else //如果原为全选状态,则更新为全不选
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[0].Value = false;
}
//下面的代码都无法彻底清空行选中状态(不是勾上)
dataGridView1.ClearSelection();
dataGridView1.CurrentCell = null;
// dataGridView1.Rows[0].Selected = false;
}
}
}
dataGridView1.Rows[i].Cells[0].Value = true;这个复值肯定和是不是选不选中状态没关系的
用cellclick或cellcontentclick事件,则不能选中表头了
e.RowIndex == -1 && e.ColumnIndex == -1
this.dataGridView1.EndEdit();//结束编辑状态,必须要此句
this.dataGridView1.Rows[i].Cells[0].Value = true;
在赋值之前加上这一句,就搞定了。
当然在更新为全不选时,以下两句都要:
dataGridView1.ClearSelection();
dataGridView1.CurrentCell = null;
{
int rIndex = dataGridView1.CurrentCell.RowIndex;
if (this.dataGridView1.Rows[rIndex].Cells[0].EditedFormattedValue.ToString() == "True")//或者=="False"
{
this.dataGridView1.Rows[rIndex].Cells[0].Value = false;
}
else
{
this.dataGridView1.Rows[rIndex].Cells[0].Value = true;
}
}