检查DataGridView是否为空

时间:2013-12-03 12:07:47

标签: c# datagridview oledb rows

我在特定列中检查我的datagridview是否为空值,但我似乎无法使其正常工作。

这是我的代码: 感谢。

        for (int i = 0; i < (DataGridView1.Rows.Count - 1); i++)
      {
          string colTimeOut = DataGridView1.Rows[i].Cells[4].Value.ToString();    
          MessageBox.Show(colTimeOut);
          if (DataGridView1.Rows[i].Cells[4].Value == null || 
          DataGridView1.Rows[i].Cells[4].Value == string.Empty ||
          DataGridView1.Rows[i].Cells[4].Value == "")

          {
              OLEDB_Connection.Open();
              updateCmd.Connection = OLEDB_Connection;
              updateCmd.CommandText = "INSERT INTO TestDB (TimeOut) VALUES (@TIMEOUT)";
              updateCmd.Parameters.AddWithValue("@TIMEOUT", varTime);
              updateCmd.ExecuteNonQuery();
              OLEDB_Connection.Close();

          }

      else  

2 个答案:

答案 0 :(得分:1)

试试这个

  for (int i = 0; i < (DataGridView1.Rows.Count); i++)
  {
      string colTimeOut = DataGridView1.Rows[i].Cells[4].Value.ToString();    
      MessageBox.Show(colTimeOut);
      if (String.IsNullOrEmpty(colTimeOut))

      {
          OLEDB_Connection.Open();
          updateCmd.Connection = OLEDB_Connection;
          updateCmd.CommandText = "INSERT INTO TestDB (TimeOut) VALUES (@TIMEOUT)";
          updateCmd.Parameters.AddWithValue("@TIMEOUT", varTime);
          updateCmd.ExecuteNonQuery();
          OLEDB_Connection.Close();

      }

  else  

答案 1 :(得分:0)

你可以这样使用 ........................

这里是演示示例,您可以根据您的使用进行修改..........

foreach(gridInvoice.Rows中的DataGridViewRow行)// grdInvoice是数据网格视图

{

                if (row.Cells[SelectColumnIndex].Value != null && Convert.ToBoolean(row.Cells[SelectColumnIndex].Value) == true)
                {
                    dbObject.AddTaskByInvoice((int)row.Cells[1].Value);

                }
            }
相关问题