private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex > -1) { var dataGrid = (DataGridView)sender; var row = dataGrid.Rows[e.RowIndex]; BackupDetailsModel backupDetailsModel = new BackupDetailsModel(); if (row != null) { if (row.Cells[0].Value != null) { backupDetailsModel.BackupAction = row.Cells[0].Value.ToString(); } if (row.Cells[1].Value != null) { backupDetailsModel.BackupType = row.Cells[1].Value.ToString(); } if (row.Cells[2].Value != null) { backupDetailsModel.BackupPattern = row.Cells[2].Value.ToString(); } // add all values to the model which will be used to persist data into Json file if (!string.IsNullOrEmpty(backupDetailsModel.BackupAction) && !string.IsNullOrEmpty(backupDetailsModel.BackupType) && !string.IsNullOrEmpty(backupDetailsModel.BackupPattern)) { _backupDetailsModelList.Add(backupDetailsModel); } } } }
// Initizialize the Backup Details grid with data saved in the json private void InitializeBackupDetails() { var persistence = new Persistence(); var request = persistence.ReadFormInformation(); var backupDetails = request != null ? request.BackupDetailsModelList != null ? request.BackupDetailsModelList.Count > 0 ? request.BackupDetailsModelList[0] != null ? request.BackupDetailsModelList.Where(b => b.BackupName.Equals(_taskName)).ToList() : null : null : null : null; if (backupDetails != null && backupDetails.Count > 0) { // create backupModel which is used as a new row where user can add another Action var emtpyModel = new BackupDetailsModel { BackupAction = string.Empty, BackupType = string.Empty, BackupPattern = string.Empty }; backupDetails.Insert(backupDetails.Count, emtpyModel); dataGridView1.DataSource = backupDetails; } else { dataGridView1.DataSource = null; dataGridView1.Rows.Clear(); dataGridView1.Refresh(); } }
private void GetBackupDetailsInfo() { Persistence persistence = new Persistence(); var request = persistence.ReadFormInformation(); if (request != null && request.BackupDetailsModelList != null) { // create backupModel which is used as a new row where user can add another Action BackupDetailsModel emtpyModel = new BackupDetailsModel { BackupAction = string.Empty, BackupType = string.Empty, BackupPattern = string.Empty }; request.BackupDetailsModelList.Insert(request.BackupDetailsModelList.Count, emtpyModel); dataGridView1.DataSource = request.BackupDetailsModelList; } }
private BackupDetailsModel SetBackupDetailsFromGrid(DataGridViewRow row) { var backupDetailsModel = new BackupDetailsModel(); backupDetailsModel.BackupName = _taskName; backupDetailsModel.TrimmedBackupName = string.Concat(_taskName.Where(c => !char.IsWhiteSpace(c))); if ((row.Cells[0].Value == null && row.Cells[1].Value == null && row.Cells[2].Value == null) || (string.IsNullOrEmpty(row.Cells[0].Value.ToString()) && string.IsNullOrEmpty(row.Cells[1].Value.ToString()) && string.IsNullOrEmpty(row.Cells[2].Value.ToString()))) { //do nothing in this case because it is about the last empty row added by datagridview } else { if (row.Cells[0].Value != null) { backupDetailsModel.BackupAction = row.Cells[0].Value.ToString(); } else { MessageBox.Show(Constants.ActionNameErrorMessage, Constants.InformativeMessage); InitializeBackupDetails(); } if (row.Cells[1].Value != null) { backupDetailsModel.BackupType = row.Cells[1].Value.ToString(); } else { MessageBox.Show(Constants.FileTypeErrorMessage, Constants.InformativeMessage); InitializeBackupDetails(); } if (row.Cells[2].Value != null) { backupDetailsModel.BackupPattern = row.Cells[2].Value.ToString(); InitializeBackupDetails(); } else { MessageBox.Show(Constants.PatternErrorMessage, Constants.InformativeMessage); InitializeBackupDetails(); } } return(backupDetailsModel); }
public void SaveDetailModel(BackupDetailsModel detailsModel) { var jsonText = File.ReadAllText(_persistancePath); var request = JsonConvert.DeserializeObject <JsonRequestModel>(jsonText); if (detailsModel != null && request.BackupDetailsModelList != null) { foreach (var item in request.BackupDetailsModelList) { if (item.BackupName.Equals(detailsModel.BackupName)) { request.BackupDetailsModelList.Remove(item); break; } } request.BackupDetailsModelList.Add(detailsModel); WriteJsonRequestModel(request); } }
// Add rows information from grid to a BindingList which will be used as data source when moving row up/down private void AddRowsInformation(BindingList <DataGridViewRow> rowList) { var bindingSource = new BindingSource(); bindingSource.DataSource = rowList; foreach (DataGridViewRow item in bindingSource) { BackupDetailsModel bdm = new BackupDetailsModel(); bdm.BackupAction = item.Cells[0].Value != null ? item.Cells[0].Value.ToString() : null; bdm.BackupType = item.Cells[1].Value != null ? item.Cells[1].Value.ToString() : null; bdm.BackupPattern = item.Cells[2].Value != null ? item.Cells[2].Value.ToString() : null; _backupDetailsModelList.Add(bdm); } dataGridView1.DataSource = _backupDetailsModelList; Persistence persistence = new Persistence(); persistence.UpdateBackupDetailsForm(_backupDetailsModelList); GetBackupDetailsInfo(); dataGridView1.ClearSelection(); }