Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

check if there are empty cells in datagridview

$
0
0


I want to get all data from my datagridview and save into my database.

But before saving into database I must check if there is an empty cell then must show an alert message and after all cells will be filled then I will save into my database.

private void save_Click(object sender, EventArgs e)
        {
            connection.Open();
            decimal[,] array = new decimal[dataGridView1.RowCount-1, dataGridView1.ColumnCount-1];
            String[] value= new String[dataGridView1.RowCount-1];

             for (int i = 0; i < array.GetLength(0); i++)
                 for (int j = 0; j < array.GetLength(1); j++)
                 {
                   if (string.IsNullOrEmpty(dataGridView1.Rows[i].Cells[j].Value.ToString())) MessageBox.Show("There is an empty cell.");
                     else array[i, j] = Convert.ToDecimal(dataGridView1.Rows[i].Cells[j].Value);
                 }

                 for (int i = 0; i < array.GetLength(0); i++)
                 {
                    OleDbCommand cmd = new OleDbCommand("INSERT into TIPOB (id,a,b,c,d) VALUES (" + getIndex() + "," + array[i, 0] + "," + array[i, 1] + "," + array[i, 2] + "," + array[i, 3] + ")", connection);
                     cmd.ExecuteNonQuery();
                 }
                 MessageBox.Show("ok");
            connection.Close();
        }
In this code I get the alert message for empty cell but sill saves into database even if there are empty cells



Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>