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 回答