// When entering a code, make sure that it is valid. private void grid_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { string newCode = e.FormattedValue.ToString(); string reason; if (!QueryEvent.IsLegalControlCode(newCode, out reason)) { // The code isn't valid. Disallow. ((MainFrame)Owner).ErrorMessage(reason); e.Cancel = true; } }