我正在尝试将数据网格整行存储在数据库中,我的数据网格有4列(组合框列,2个文本框列,复选框列),我在数据网格的第3列中触发单元格离开事件,并存储整行值进入数据库,但是i; m没有得到最后一个单元格的编辑值并得到异常,这是我的代码
private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if ( dataGridView1.CurrentCell == dataGridView1[dataGridView1.ColumnCount - 2, dataGridView1.RowCount - 1])
{
try
{
SqlParameter p1, p2, p3, p4, p5;
p1 = cmd.Parameters.Add("@tid", SqlDbType.VarChar, 20);
p1.Value = dataGridView1.Rows[j - 1].Cells[0].Value;
MessageBox.Show(dataGridView1.Rows[j - 1].Cells[0].Value.ToString());
p2 = cmd.Parameters.Add("@uid", SqlDbType.VarChar, 20);
p2.Value = "D";
p3 = cmd.Parameters.Add("@timespent", SqlDbType.Time);
p3.Value = dataGridView1.Rows[j - 1].Cells[1].Value;
MessageBox.Show(dataGridView1.Rows[j - 1].Cells[1].Value.ToString());
p4 = cmd.Parameters.Add("@taskdate", SqlDbType.Date, 20);
p4.Value = dateTimePicker1.Value.Date;
p5 = cmd.Parameters.Add("@status", SqlDbType.VarChar, 20);
p5.Value = dataGridView1.Rows[j-1].Cells[2].Value;
MessageBox.Show(dataGridView1.Rows[j - 1].Cells[2].Value.ToString());
cmd.ExecuteNonQuery();
MessageBox.Show("successfully Entered");
con1.Close();