Ryan Gillooly
Ryan Gillooly

Reputation: 315

Check DataGridView for Nulls

I am checking my datagridview in a specific column for a null value, but i cant seem to get it working.

Here is my code : Thanks.

        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  

Upvotes: 0

Views: 60

Answers (2)

tariq
tariq

Reputation: 2258

try this

  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  

Upvotes: 1

Hozefa Laxmidhar
Hozefa Laxmidhar

Reputation: 102

for that you can use like this ........................

here it is demo example you can modify according to your use ..........

foreach (DataGridViewRow row in gridInvoice.Rows) // grdInvoice is data grid view

{

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

                }
            }

Upvotes: 0

Related Questions