if (MessageBox.Show("确定要删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }            int chkCount = 0;            for (int i = 0; i < this.dgvGatherInfo.Rows.Count; i++)
            {
                if (this.dgvGatherInfo.Rows[i].Cells[0].Value != null) //判断该行的复选框是否存在 
                {
                    if (this.dgvGatherInfo.Rows[i].Cells[0].Value.ToString() == "True") //判断该复选框是否被选中 
                    {
                        gaRule.GatherInfoDel(Convert.ToInt32(this.dgvGatherInfo.Rows[i].Cells[1].Value));
                    }
                    chkCount++;
                }
            }
            if (chkCount == 0)
            {
                MessageBox.Show("请选择要删除的行!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            MessageBox.Show("删除成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.FormGatherInfo_Load(this,null);
生成通过,运行程序的话在checkbox选择,要选两个或两个以上才能执行删除,选一个的话会提示叫你“选择要删除的行”
调试的时候 只在一个checkbox打勾,程序运行到if (this.dgvGatherInfo.Rows[i].Cells[0].Value != null) //判断该行的复选框是否存在  就会直接跳过到if (chkCount == 0) 
不知道怎么回事
大家帮忙看下

解决方案 »

  1.   

     if (this.dgvGatherInfo.Rows[i].Cells[0].Value.ToString()  != "") //判断该行的复选框是否存在 
    试试
      

  2.   

    if (this.dgvGatherInfo.Rows[i].Cells[0].Value != null) //判断该行的复选框是否存在
                    {
                        if (this.dgvGatherInfo.Rows[i].Cells[0].Value.ToString() == "True") //判断该复选框是否被选中
                        {
                            gaRule.GatherInfoDel(Convert.ToInt32(this.dgvGatherInfo.Rows[i].Cells[1].Value));
                        }
                        chkCount++;
                    } 
    在if前加个判断,判断当前行的类型是否为数据行,如果为表头的话。肯定不能算。。
      

  3.   

     if (this.dgvGatherInfo.Rows[i].Cells[0].Value.ToString() == "True") //判断该复选框是否被选中 
    可以这样来判断吗?我一般是用if (Convert.ToBoolean(dgr.Cells["是否选择"].Value) == false)
      

  4.   

    if (this.dgvGatherInfo.Rows[i].Cells[0].FindControl["CheckBox"]!= null)
     if(找到的CheckBox.checked==true){...}
      

  5.   

    为什么调试时候不看看this.dgvGatherInfo.Rows[i].Cells[0].Value 的值呢~~
    放到监视里就可以看到的,我觉也是表头的问题
      

  6.   

    for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                bool isChecked = ((CheckBox)GridView1.Rows[i].FindControl("CheckBox1")).Checked;
                if (isChecked)//找到模板中的CheckBox
                {
                    numOfChecked += 1;//计数
                }
            }
            if (numOfChecked == 1)
            {
                for (int i = 0; i < this.GridView1.Rows.Count; i++)
                {
                    bool isChecked = ((CheckBox)GridView1.Rows[i].FindControl ("CheckBox1")).Checked;
                    if (isChecked)
                    {
                      //有选中。
                    }            }        }
            else
            {
                Response.Write(" <Script language=JavaScript>alert('您只能选择一条记录!');
                                </Script>");
     
            }
      

  7.   

    因为你的 this.dgvGatherInfo.Rows[i].Cells[0].Value 一直是空的,判断该行的复选框是否存在  是你这样判断的吗?
    你用的DataGridView?
      

  8.   

    选择任意一个的时候
    调试看到的this.dgvGatherInfo.Rows[i].Cells[0].Value 的值都是null
    不知道怎么回事那个表头是什么意思啊
      

  9.   

    你先可以自己测试下,简单的就判断下是否选中?还有你的复选框是怎么添加的?我说了你的代码具体写在哪里了?
    private void button1_Click_1(object sender, EventArgs e)
            {
                int chkCount = 0;            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                {
                    if (this.dataGridView1.Rows[i].Cells[0].Value != null) //判断该行的复选框是否存在 
                    {
                        if (this.dataGridView1.Rows[i].Cells[0].Value.ToString() == "True") //判断该复选框是否被选中 
                           {
                            MessageBox.Show("choose");
                        }
                        chkCount++;
                    }
                    if (chkCount == 0)
                    {
                        MessageBox.Show("fail");
                    }
             }
      

  10.   

    对啊
    我又设置断点调试了下
    说错了
    选中N个checkbox 执行删除
    只会删除前N-1条记录
      

  11.   

    如果是多选框.你最好先找到以后再判断是否选中:
      if (this.dgvGatherInfo.Rows[i].Cells[0].Value != null) //判断该行的复选框是否存在 
                    { 
                        if (this.dgvGatherInfo.Rows[i].Cells[0].Value.ToString() == "True") //判断该复选框是否被选中 
                        { 
                            gaRule.GatherInfoDel(Convert.ToInt32(this.dgvGatherInfo.Rows[i].Cells[1].Value)); 
                        } 
    -------------------------------------------------------------------------------------------------------------------
    CheckBox cb1 = (CheckBox)(GridView1.Rows[i].Cells[0].FindControl("多选框ID"));
                        if (cb1.Checked)
                        {
                           gaRule.GatherInfoDel(Convert.ToInt32(this.dgvGatherInfo.Rows[i].Cells[1].Value))
                        }当然,这句你要判断下:
      

  12.   

    你也可以试试改成这样:CheckBox cb1 = (CheckBox)(dgvGatherInfo.Rows[i].Cells[0].ConTrols[0]); 
                        if (cb1.Checked) 
                        { 
                          gaRule.GatherInfoDel(Convert.ToInt32(this.dgvGatherInfo.Rows[i].Cells[1].Value)) 
                        } 
      

  13.   

    为什么不通过BindingSource之类的删?至少我是不会用这种方式去删数据的。难道微软的数据库与界面控件的双向数据绑定是白给的吗?INotifyPropertyChanged这东西是摆设吗?
      

  14.   

    你的datagridview是不是有自动增长列的?
      

  15.   

    在执行循环删除的时候设置下从第一行开始
    this.dgvGatherInfo.rowType不等于表头
      

  16.   

    不是啊
    我加载窗体的时候已经设置为false了
      

  17.   

    checkbox是在datagridview编辑列上面添加的
    上面这段代码是删除按钮事件的代码
      

  18.   

    /// <summary>
            /// 删除按钮
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void toolStripButtonDel_Click(object sender, EventArgs e)
            {
                if (MessageBox.Show("确定要删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    return;
                }            int chkCount = 0;            for (int i = 0; i < dgvGatherInfo.Rows.Count; i++)
                {
                    if (dgvGatherInfo.Rows[i].Cells[0].Value != null) //判断该行的复选框是否存在 
                    {
                        if (dgvGatherInfo.Rows[i].Cells[0].Value.ToString() == "True") //判断该复选框是否被选中 
                        {
                            gaRule.GatherInfoDel(Convert.ToInt32(dgvGatherInfo.Rows[i].Cells[1].Value));
                        }
                        chkCount++;
                    }
                }
                if (chkCount == 0)
                {
                    MessageBox.Show("请选择要删除的行!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                MessageBox.Show("删除成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.FormGatherInfo_Load(this,null);
            }
      

  19.   

    我这选择一个都不会是NULL的啊?
      

  20.   

    你的AllowUserToAddRows属性有没有设置为false啊
      

  21.   

    好象以前出现过类似问题
    后来做了个投机的办法,就是把第一行的那个有checkbox的那一列选中下,这样就可以删除掉了
    dgvGatherInfo.Rows[0].Cells[1].Selected = true;
    不知道楼主是不是这种情况
      

  22.   

    AllowUserToAddRows设置为false,这个可能是主要原因。
    如果还不行,试试:
     dataGridView1.BeginEdit(true);和
     if (dataGridView1.Rows[e.RowIndex].Cells["Column1"].Value.ToString() == "true")
    这两个代码加入
      

  23.   

    gaRule.GatherInfoDel(Convert.ToInt32(dgvGatherInfo.Rows[i].Cells[1].Value));
    这句好像有问题吧。。选中的第一个用这句应该不可以删的
      

  24.   

    向这种问题断点调试一下就行了if (this.dgvGatherInfo.Rows[i].Cells[0].Value.ToString() == "True") //判断该复选框是否被选中 
             { 
                            gaRule.GatherInfoDel(Convert.ToInt32(this.dgvGatherInfo.Rows[i].Cells[1].Value)); 
                            chkCount++; 
            } 
                        
      

  25.   

      string idStr = "";
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox newsCheck = (CheckBox)row.FindControl("CheckBox1");
                if (newsCheck != null)
                {
                    if (newsCheck.Checked == true)
                    {
                        idStr += GridView1.DataKeys[row.RowIndex].Value.ToString() + ",";
                    }
                }
            }
      

  26.   

    解决了
    我添加了datagridview一个单击cell命令
    /// <summary>
            /// 单击cell命令
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void dgvGatherInfo_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == 0)
                {
                    this.dgvGatherInfo.Rows[e.RowIndex].Cells[0].Value = this.dgvGatherInfo.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString() == "True" ? "False" : "True";
                }
            }谢谢大家的帮忙