protected internal void ValidateForEmptyKeys(ref DataGridView dgv, ref ValidationResult result) { var numberOfEmptyKeys = dgv.Rows.Cast<DataGridViewRow>() .Count(x => string.IsNullOrWhiteSpace((string) x.Cells[0].FormattedValue)); if (numberOfEmptyKeys > 1) { result.ErrorMessages.Add(string.Format("{0} empty key(s) found.", numberOfEmptyKeys - 1)); } }
protected internal void ValidateForInvalidDirectories(ref DataGridView dgv, ref ValidationResult result) { // grab all the directory paths var badDirectoryPaths = dgv.Rows.Cast<DataGridViewRow>() .Where( r => ((string) r.Cells[0].FormattedValue) != string.Empty && ((string) r.Cells[1].FormattedValue) != string.Empty) // do not capture the "add new" row .Where(r => !Directory.Exists((string) r.Cells[1].FormattedValue)) .Select(x => ((string) x.Cells[1].FormattedValue)) .ToList(); // append error message for those paths which are invalid foreach (var badDirectoryPath in badDirectoryPaths) { result.ErrorMessages.Add(string.Format("Invalid path: \"{0}\"", badDirectoryPath)); } }
protected internal void ValidateForUniqueKeyValues(ref DataGridView dgv, ref ValidationResult result) { // do a "groupby" to count the number of times each unique string shows up in our key column var groupByResult = dgv.Rows.Cast<DataGridViewRow>() .Where(r => ((string) r.Cells[0].FormattedValue) != string.Empty) .GroupBy(x => ((string) x.Cells[0].FormattedValue)) .Select(x => new {key = x.Key, number = x.Count()}); // ensure each string shows up in key list only once. if (groupByResult.Count(x => x.number > 1) > 0) result.ErrorMessages.Add("Duplicate keys found."); }
/* private void dgvConfigSectionLocations_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { var grid = (DataGridView) sender; if (grid.IsCurrentRowDirty) { var row = dgvConfigSectionLocations.Rows[e.RowIndex]; var keyCell = row.Cells[0]; var valueCell = row.Cells[1]; //e.Cancel = !(IsKeyValid(keyCell) && IsValueValid(valueCell)); IsKeyValid(keyCell); IsValueValid(valueCell); } } private void dgvConfigSectionLocations_RowValidated(object sender, DataGridViewCellEventArgs e) { errorProvider1.SetError(ddlEnvironmentName, string.Empty); foreach (DataGridViewCell cell in dgvConfigSectionLocations.Rows[e.RowIndex].Cells) { cell.ErrorText = string.Empty; } } private bool IsKeyValid(DataGridViewCell cell) { if(string.IsNullOrWhiteSpace(cell.FormattedValue.ToString())) { cell.ErrorText = "Required"; } else { var count = dgvConfigSectionLocations.Rows .Cast<DataGridViewRow>() .Where(x => x.Cells[0].FormattedValue.ToString() == cell.FormattedValue.ToString()) .Count(); if (count > 1) cell.ErrorText = "Duplicate key."; } return (cell.ErrorText.Length == 0); } private bool IsValueValid(DataGridViewCell cell) { var result = true; if (!Directory.Exists((string)cell.FormattedValue)) { cell.ErrorText = "Invalid Directory path."; result = false; } return result; } */ private ValidationResult ValidateForm() { var environmentNameValidation = ValidateEnvironmentName(); var environmentRoleValidation = ValidateEnvironmentRole(); var configSectionLocationsGridValidation = ValidateConfigSectionLocationsGrid(); var result = new ValidationResult(); result.ErrorMessages = environmentNameValidation.ErrorMessages .Concat(environmentRoleValidation.ErrorMessages) .Concat(configSectionLocationsGridValidation.ErrorMessages) .ToList(); return result; }
private ValidationResult ValidateEnvironmentRole() { var result = new ValidationResult(); return result; }
private ValidationResult ValidateEnvironmentName() { var result = new ValidationResult(); if (ddlEnvironmentName.Text.Trim().Length == 0) { result.ErrorMessages.Add("Environment Name is a required field."); errorProvider1.SetError(ddlEnvironmentName, "Required"); } else { errorProvider1.SetError(ddlEnvironmentName, string.Empty); } return result; }
private ValidationResult ValidateConfigSectionLocationsGrid() { var result = new ValidationResult(); // Check for duplicate key values ValidateForUniqueKeyValues(ref dgvConfigSectionLocations, ref result); // Check for empty keys ValidateForEmptyKeys(ref dgvConfigSectionLocations, ref result); // Check for invalid directory paths in value column ValidateForInvalidDirectories(ref dgvConfigSectionLocations, ref result); errorProvider1.SetError(dgvConfigSectionLocations, result.IsValid ? string.Empty : result.ErrorMessages.Aggregate((current, next) => current + "\r\n" + next)); return result; }