Situation: I have placed a DataGridView on a form. In the DataGridView-Object I do checks on some cells - like for example if the amount which was entered by the user is not greater than 100. The checks will be performed when the user leaves the cell with Enter, Tab or the Arrow Keys.
Problem: Everything is working fine but when the cursor is in the cell and the value is greater than 100 and the User presses the "X"-Button on the form (Close-Button) the Message still appears.
Question: How can I prevent the appearance of the MessageBox when the User is clicking on the X-Button on the form?
Code Samples:
private void dgv_CellValidated(object sender, DataGridViewCellEventArgs e)
{
try
{
const int nColumn = 2;
if (!e.ColumnIndex.Equals(nColumn))
{
return;
}
if (e.ColumnIndex.Equals(nColumn))
{
double nMengeSource;
double.TryParse(dgv.Rows[e.RowIndex].Cells[fldMenge.Name].Value.ToString(),
out nMengeSource);
double nMengeLos;
double.TryParse(dgv.Rows[e.RowIndex].Cells[fldMengeLos.Name].Value.ToString(),
out nMengeLos);
// prüfe ob erfasste Menge die Menge im Los überschreitet
if (nMengeSource > nMengeLos)
{
var sMsg = String.Empty;
sMsg += "Warning! Value is greather than allowed!";
MessageBox.Show(sMsg, "Check...", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch (Exception ex)
{
Dialog.SetDefaultCursor();
MessageBox.Show(MethodBase.GetCurrentMethod().Name + @"\n\n" + ex + @"\n\n" + ex.Message);
}
}