anna john

anna john

  • NA
  • 47
  • 9.2k

row validating not letting form to close if a new row added

Oct 11 2019 5:17 PM
after validating if all fields are available in a datagridview row, using RowValidating event; if the user accidently clicks on a new row, then the validations are triggered and not letting form to close. 
 
Below is the code 
private void dgv1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
if (dgv1.CurrentRow != null)
{
DataGridViewRow dgvRow = dgv1.CurrentRow;
if (dgvRow.Cells["txtEmpName"].Value == DBNull.Value)
{
MessageBox.Show("Employee Name cannot be empty");
e.Cancel = true;
}
}
}
the user enters one row successfully and clicks on next row. then row validation triggers and not letting form to close.  
 

Answers (3)