/// <summary> /// Loads table from XML file. /// </summary> private void LoadFromXML() { if (!File.Exists(XmlPath)) { MainGridView.Rows.Clear(); return; } RefreshDataGridView(MainGridView); using (DataSet set = new DataSet()) { set.ReadXml(XmlPath, XmlReadMode.ReadSchema); MainGridView.DataSource = set.Tables[0]; foreach (DataGridViewColumn column in MainGridView.Columns) { column.HeaderText = set.Tables[0].Columns[MainGridView.Columns.IndexOf(column)].Caption; } } ChangedCells.Clear(); BadCells.Clear(); SetButtons(false, false, true); LoadSettings(); }
/// <summary> /// Retrieves table from database. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void LoadTableFromDatabase(object sender, EventArgs e) { using (GetUserServiceClient client = new GetUserServiceClient("NetTcpBinding_IGetUserService")) { DataSet set = client.GetTable(); if (set.DataSetName == "Error") { MessageBox.Show("Client could not connect to database. Server is either unavailable or you are not connected to the internet.", "Connection error"); return; } DataTable table = set.Tables[0]; RefreshDataGridView(MainGridView); MainGridView.DataSource = table; foreach (DataGridViewColumn column in MainGridView.Columns) { column.HeaderText = table.Columns[MainGridView.Columns.IndexOf(column)].Caption; } } ChangedCells.Clear(); BadCells.Clear(); SetButtons(true, true, true); LoadSettings(); Output("Loaded data from database."); }
/// <summary> /// Changes given cell based on it's formatting /// </summary> /// <param name="cell"></param> /// <param name="isFormattingCorrect"></param> private void SetCell(DataGridViewCell cell, bool isFormattingCorrect) { if (isFormattingCorrect) { if (BadCells.Contains(cell)) { BadCells.Remove(cell); } cell.Style.BackColor = Color.PaleGreen; } else { if (!BadCells.Contains(cell)) { BadCells.Add(cell); } cell.Style.BackColor = Color.IndianRed; } if (MainGridView.Columns[cell.ColumnIndex].Name == "Apartment") { if (!ChangedCells.Contains(cell)) { cell.Style.BackColor = Color.Empty; } } }
/// <summary> /// Sets changed cell's colors to white and clears the list. /// </summary> private void ClearChangedCells() { foreach (DataGridViewCell cell in ChangedCells) { cell.Style.BackColor = Color.Empty; } ChangedCells.Clear(); }
private void DataGridView2_CellEndEdit(object sender, DataGridViewCellEventArgs e) { DataGridViewCell currentCell = MainGridView[e.ColumnIndex, e.RowIndex]; //Prevents cell from staying white after it was edited if (ChangedCells.Contains(currentCell)) { SetCell(currentCell, CheckCell(currentCell)); } }
/// <summary> /// Deletes content of given DataGridView. /// </summary> /// <param name="gridView"></param> private void RefreshDataGridView(DataGridView gridView) { gridView.Columns.Clear(); DataTable DT = (DataTable)gridView.DataSource; DT?.Clear(); BadCells.Clear(); ChangedCells.Clear(); gridView.Refresh(); }
private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0) { return; } DataGridViewCell currentCell = MainGridView[e.ColumnIndex, e.RowIndex]; bool isInputCorrect = CheckCell(currentCell); SetCell(currentCell, isInputCorrect); ChangedCells.Add(currentCell); SetButtons(!BadCells.Any(), true, !BadCells.Any()); }
/// <summary> /// Remove cells inside the given row. /// </summary> /// <param name="row"></param> private void DeleteCellsFromRow(DataGridViewRow row) { foreach (DataGridViewCell cell in row.Cells) { if (ChangedCells.Contains(cell)) { ChangedCells.Remove(cell); } if (BadCells.Contains(cell)) { BadCells.Remove(cell); } } if (!BadCells.Any()) { SetButtons(true, true, true); } }