Qing Ryder

Qing Ryder

  • NA
  • 202
  • 17.4k

Validating Datagridview in a form.

Oct 12 2017 10:57 AM
i have a form, with datagrid where inputs are collected, together with a submission button. at the submission of this data, i want to validate the datagrid to check for empty cells.
from my code, the datagrid wont be validated until i have exited then it keeps bugging me with the messagebox message.
 
here is my code:
 
  1. private void button3_Click(object sender, EventArgs e)  
  2.        {  
  3.            foreach (DataGridViewRow rw in this.dataGridView1.Rows)  
  4.            {  
  5.                for (int i = 0; i < rw.Cells.Count; i++)  
  6.                {  
  7.                    if (rw.Cells[i].Value == null || rw.Cells[i].Value == DBNull.Value || String.IsNullOrWhiteSpace(rw.Cells[i].Value.ToString()))  
  8.                    {  
  9.                        string message = "Sorry, But some Fields are Empty, Therefore, you cannot Proceed";  
  10.                        string caption = "Error Detected in Input";  
  11.                        MessageBoxButtons buttons = MessageBoxButtons.OK;  
  12.                        DialogResult result;  
  13.   
  14.                        // Displays the MessageBox.  
  15.   
  16.                        result = MessageBox.Show(message, caption, buttons);  
  17.   
  18.                        if (result == System.Windows.Forms.DialogResult.OK)  
  19.                        {  
  20.                            return;  
  21.                        }  
  22.                    }  
  23.                    else  
  24.                    {  
  25.                        resultVV f3 = new resultVV();  
  26.                        f3.ShowDialog();  
  27.                        this.Hide();  
  28.                    }  
  29.                }  
  30.            }  
  31.        } 

Answers (1)