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");  

保存する前に確認する方法を誰か教えてください

4

2 に答える 2

1

そのようなことを試してください

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

それがうまくいくことを願っています。

于 2013-05-27T07:24:54.450 に答える
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

         }
   }
于 2013-05-27T07:26:46.330 に答える