代码如下:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
this.dataGridView1.Rows[i].Cells[0].Value = true;
}
}
else
{
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
this.dataGridView1.Rows[j].Cells[0].Value = false;
}
}
}
private void button10_Click(object sender, EventArgs e)
{
string str = "";
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if(Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value))
{
str += dataGridView1.Rows[i].Cells[1].Value + ",";
}
}
MessageBox.Show(str);
}
注意:1,标题栏 添加全选,需要重写,非常繁琐。网上文章也不是很多。
2,dataGridView1.Rows[i].Cells[0].Value 得到是那一列的值,还是 那一列中的 Checkbox的值 ?不需要考虑
3,添加的 Checkbox 需要做什么设置 ? 需要设置TrueValue,FalseValue ? 不需要设置
4,添加的checkbox 那列,只读属性一定要去掉,不然选中之后,失去焦点就还原了。
5,checkbox 那列,需要设置宽度吗? 用户点击是文本区域还是Checkbox ? 没有要求。
6,dataGridView1.Rows[i].Cells[0].Value 不要轻易的把这个值 转换成String类型。 因为他有 N 种情况。通过 Convert.ToBoolean
强制类型转换,最终只有2种状态 True,False。
7,批量删除 ,用 in
string orderid="11,22,33,55";
SqlParameter[] parms = {new SqlParameter("id",orderid) };
另外一种删除 delete from aa whereid=@id1 /r/n delete from aa whereid=@di2
SqlParameter[] parms = {new SqlParameter("id1","11") ;new SqlParameter("id2","22") };
8, 上面写了那么多,其实对我现在遇到的问题 没有任何帮助。
如果是新项目,可以用上面的。老项目(不停的炒现饭那种),可能会遇到 选中的值 dataGridView1.Rows[i].Cells[0].Value=“”
这样转 BOOL 类型就会报错。 目前还没有知道什么原因,临时解决办法
首先 gridViewA 属性需要设置 TrueValue=1,FalseValue=0 然后取值如下
if (gridViewA.Rows[i].Cells[0].Value != null)
{
if (gridViewA.Rows[i].Cells[0].Value.ToString() == "1" || gridViewA.Rows[i].Cells[0].Value.ToString()=="True")
{
id += gridViewA.Rows[i].Cells[1].Value + ",";
}
}