2013-05-27 53 views
0

您好我有3个柱面的粗糙视图我想检查列2 & 3是否为空显示消息说在执行保存代码之前,但我不能在这里做到这一点是我的代码保存如何在保存到数据库之前检查单元格是否为空

 //second part 
     for (int i = 0; i < dataGridView1.Rows.Count; i++) 
     { 
      condatabase.Open(); 

      string Query1 = "insert into mal_makbodatsandok_det (ERADID,ERADTYPENAME,BAYAN,MONY) values('"+txtID.Text+"','" + this.dataGridView1.Rows[i].Cells[1].Value + "','" + this.dataGridView1.Rows[i].Cells[2].Value + "','" + this.dataGridView1.Rows[i].Cells[3].Value + "') ;"; 


      SqlCommand cmddatabase = new SqlCommand(Query1, condatabase); 
      myreader = cmddatabase.ExecuteReader(); 

      while (myreader.Read()) 
      { 

      } 

      condatabase.Close(); 
    } 

     MessageBox.Show("saved"); 

PLZ有人告诉我如何保存

回答

1

尝试类似的东西

if (dataGridView1.CurrentCell.Value == DBNull.Value) 
{ 
    MessageBox.Show("null"); 
} 
else 
{ 
    MessageBox.Show("not null"); 
} 

希望我前检查吨工作。插入前

0

调用方法

if(ValidateGridView() != -99) 
    { 
    //Insert Logic 
    } 
    else 
    { 
    //display error message 
    } 



    private int ValidateGridView() 
    { 
     for (int i = 0; i < dataGridView1.Rows.Count; i++) 
     { 
      if(this.dataGridView1.Rows[i].Cells[1].Value != string.Empty && 
     this.dataGridView1.Rows[i].Cells[2].Value != string.Empty && 
     this.dataGridView1.Rows[i].Cells[3].Value != string.Empty) 
      {} 
      else 
      { 
      return i; 
       //Return line number and display message, fields on this row number can not be empty 
      } 
     return -99; //if return is -99 means no row has empty value 

     } 
    } 
相关问题