1
Answer

Validating Datagridview in a form.

qing ryder

qing ryder

7y
219
1
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)
1
Amit Gupta

Amit Gupta

NA 16.5k 25.5k 7y
Hey
 
Please avoid validating throughout your datagrid, use CellEditEnd event of the datagrid, and perform validation there and revert the changes if it doesnot full fill your requirement.
 
Refer
 https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellendedit(v=vs.110).aspx