3
Answers

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

anna john

anna john

5y
732
1
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)