2013-05-27 4 views
0

안녕하세요 저는 3 열로 agrid 뷰를 가지고 있습니다. 열 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 사람이 어떻게

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

희망 내가 뭔가를 시도

답변

1

을 저장하기 전에 확인하는 말해 작동합니다. 삽입 전에

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 

     } 
    } 
관련 문제