private void frmCredentials_Load(object sender, EventArgs e) { // Populate grid clsConfig.CredentialCollection cc = cfg.CredentialList; // Sort the collection before populating the grid cc.SortByMaskedIP(); foreach (clsConfig.SecurityCredential c in cc) { DataGridViewRow r = grdCredentials.Rows[grdCredentials.Rows.Add()]; r.Cells["crNetwork"].Value = c.ip.ToString(); r.Cells["crNetmask"].Value = c.netmask; r.Cells["crDescription"].Value = c.description; r.Cells["crDomain"].Value = c.domain; r.Cells["crUser"].Value = c.username; r.Cells["crPassword"].Value = c.password; } // Sort the datagrid grdCredentials.Sort(new SortByMaskedIP(crNetwork.Index, crNetmask.Index)); // Set default selected cell to the first column of the last row grdCredentials.CurrentCell = grdCredentials.Rows[grdCredentials.Rows.Count - 1].Cells[0]; }
private void btnSave_Click(object sender, EventArgs e) { // Sort the datagrid grdCredentials.Sort(new SortByMaskedIP(crNetwork.Index, crNetmask.Index)); // Validate each row bool ok = true; foreach (DataGridViewRow r in grdCredentials.Rows) { if (!r.IsNewRow && !ValidateRow(r)) { ok = false; break; } } // Save without prompting if all rows are OK. Prompt if not all rows are OK. if (ok || (!ok && MessageBox.Show("Not all rows are valid - rows with errors (highlighted) will not be saved if you continue. Save security credentials?", "Not all rows are valid", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)) { clsConfig.CredentialCollection cc = CollateForm(); // Sort the collection, then save it. cc.SortByMaskedIP(); cfg.CredentialList = cc; // Close form this.Close(); } }