locked
check if there are empty cells in datagridview RRS feed

  • Question


  • 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


    • Moved by CoolDadTx Thursday, October 22, 2015 4:46 PM Winforms related
    Thursday, October 22, 2015 6:45 AM

Answers

  • Hello,

    You would be better off setting the DataSource of the DataGridView to a DataTable. Now when you are ready to save data cast the data source to a DataTable and check for proper values e.g. 

    if(!String.IsNullOrWhiteSpace(row.Field<string>("some field")))

    Same can be done with other types e.g. Type.TryParse where Type might be int, decimal, DateTime etc.

    Of course you could setup classes via data wizards in the IDE and setup rules within these classes so that for instance a row can't be saved in the DataGridView with say empty values.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.
    Microsoft Developer tools
    Developer’s Guide to Windows 10 video series

    Thursday, October 22, 2015 9:31 AM

All replies

  • You are checking for empty cells but you do not do any control action against it. 

    Check if there is an empty cell and do add datagridview values into your database if your DGV contains empty cell: 

    bool HasEmptyCell = false;

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

    // instead of showing a messagebox, you can change the cell color

    dataGridView1.Rows[i].Cells[j].Style.ForeColor = Color.Red;

    HasEmptyCell = true;

    } else array[i, j] = Convert.ToDecimal(dataGridView1.Rows[i].Cells[j].Value); }

    }

    // if the DGV has no empty cell than you can save it to the Database

    if(!HasEmptyCell){

     connection.Open();

    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();
                     }

    connection.Close();

    }


    Thursday, October 22, 2015 7:42 AM
  • All that nasty casting and stuff is why people like orm like entity framework.
    Way more developer friendly, if there's a driver for your database.

    Hope that helps.

    Technet articles: WPF: MVVM Step 1; All my Technet Articles

    Thursday, October 22, 2015 8:01 AM
  • Hello,

    You would be better off setting the DataSource of the DataGridView to a DataTable. Now when you are ready to save data cast the data source to a DataTable and check for proper values e.g. 

    if(!String.IsNullOrWhiteSpace(row.Field<string>("some field")))

    Same can be done with other types e.g. Type.TryParse where Type might be int, decimal, DateTime etc.

    Of course you could setup classes via data wizards in the IDE and setup rules within these classes so that for instance a row can't be saved in the DataGridView with say empty values.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.
    Microsoft Developer tools
    Developer’s Guide to Windows 10 video series

    Thursday, October 22, 2015 9:31 AM