datagridview中我选择的是DataGridViewCheckBoxColumn类型,要的结果是如果选中我要得到1,如果没选中得到0,但现在当我把选中的标记取消后再保存,就会出现“ 该字符串未被识别为有效的字符串”这样的错误。
下面是相应的存储过程:
create proc proc_UpdateDuty         ----------------------根据员工编号录入周六值班信息
(
  @UserID varchar(100),                -------------------员工编号
  @UserDutyDate datetime,              -------------------要录入值班信息的日期。格式如:2008-07-01 00:00:00。
  @UserDuty int                        -------------------是否值班。值班:1;休息 0。
)
as
begin
  update   tb_WeekEndDuty set UserDuty=@UserDuty where UserID=@UserID and Year(UserDutyDate)=Year(@UserDutyDate) and Month(UserDutyDate)=Month(@UserDutyDate) and Day(UserDutyDate)=Day(@UserDutyDate)
end
go
     这是更改数据的方法:
  private void UpdateDuty(string UserID, DateTime UserDutyDate, int UserDuty)
        {
            SqlParameter[] sp = new SqlParameter[] { 
                                                        new SqlParameter("@UserID",SqlDbType.VarChar,100),
                                                        new SqlParameter("@UserDutyDate",SqlDbType.DateTime),
                                                        new SqlParameter("@UserDuty",SqlDbType.Int)
                                                    };
            sp[0].Value = UserID;
            sp[1].Value = UserDutyDate;
            sp[2].Value = UserDuty;
            db.Exec_SQL("proc_UpdateDuty", sp);
        }调用上面的方法:
 private void button7_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;            foreach (DataGridViewRow dgvRow in this.dgvSAT_Officer.Rows)
            {
                if (dgvRow.Index != -1)
                {
                   this.UpdateDuty(dgvRow.Cells[0].Value.ToString(), DateTime.Parse(this.dgvSAT_Officer.Columns[3].HeaderText.ToString()), bool.Parse(dgvRow.Cells[3].Value.ToString()) ? 1 : 0);
                    this.UpdateDuty(dgvRow.Cells[0].Value.ToString(), DateTime.Parse(this.dgvSAT_Officer.Columns[4].HeaderText.ToString()), bool.Parse(dgvRow.Cells[4].Value.ToString()) ? 1 : 0);
                    this.UpdateDuty(dgvRow.Cells[0].Value.ToString(), DateTime.Parse(this.dgvSAT_Officer.Columns[5].HeaderText.ToString()), bool.Parse(dgvRow.Cells[5].Value.ToString()) ? 1 : 0);
                    this.UpdateDuty(dgvRow.Cells[0].Value.ToString(), DateTime.Parse(this.dgvSAT_Officer.Columns[6].HeaderText.ToString()), bool.Parse(dgvRow.Cells[6].Value.ToString()) ? 1 : 0);
                    if (this.dgvSAT_Officer.Columns[7].Visible)
                    {
                        this.UpdateDuty(dgvRow.Cells[0].Value.ToString(), DateTime.Parse(this.dgvSAT_Officer.Columns[7].HeaderText.ToString()), bool.Parse(dgvRow.Cells[7].Value.ToString()) ? 1 : 0);
                    }
                }
            }            this.BindDGView("职员", this.dgvSAT_Officer);
            this.BindWork("周六", this.crystalReportViewer2, "职员");            this.Cursor = Cursors.Default;
            MessageBox.Show("职员周六值班排班信息已成功保存!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }
 

解决方案 »

  1.   

    你把bool.Parse(dgvRow.Cells[3].Value.ToString()) ? 1 : 0这段改了
    改成把
    int flag=0;
    DataGridViewCheckBoxColumn colunm = (DataGridViewCheckBoxColumn)dgvRow.Cells[3];
    if(colunm.Selected)
    {
        flag = 1;
    }
    然后
    this.UpdateDuty(dgvRow.Cells[0].Value.ToString(), DateTime.Parse(this.dgvSAT_Officer.Columns[7].HeaderText.ToString()), flag); 大概意思是这样
      

  2.   

    DataGridViewCheckBoxColumn 未选中时是null值所在你parse的时候会有问题
      

  3.   

    对啊,在编辑列中把这个字段的其中一个属性改为False就行了,问题解决了,多谢各位