/// <summary> /// When the "Remove Entry" button is clicked, remove the selected employee and customer entries, /// along with the related schedule and attendance entries respectively, from the database and from the /// DataSet. Update the grid to reflect these changes. /// </summary> private void RemoveButton_Click(object sender, EventArgs e) { try { // Get selected row. GridRangeInfo info = databaseGrid.TableModel.Selections.GetSelectedRows(true, true)[0]; // Get an element fromt the row. Element element = databaseGrid.TableModel.GetDisplayElementAt(info.Top); // Get the table the element belongs to. GridNestedTable gnt = element as GridNestedTable; // Get the data of the row. DataRowView drv = gnt.ChildTable.ParentTable.CurrentElement.GetData() as DataRowView; // Get the name of the table the row belongs to. string table = gnt.ChildTable.Name; if (table == "Employees") { // Remove the employee and corresponding employee schedule entry from the database. DatabaseWorker.RemoveEmployee(Convert.ToInt32(drv[0])); // Get the grid employee entry. Record record = gnt.ChildTable.ParentTable.CurrentElement.GetRecord(); // Get the index of the employee entry. // This index is the same as the index of the corresponding employee schedule entry. int index = databaseGrid.GetTable("Employees").UnsortedRecords.IndexOf(record); // Delete the corresponding employee schedule entry from the grid. databaseGrid.GetTable("Employee Schedule").UnsortedRecords[index].Delete(); // Delete the employee entry from the grid. record.Delete(); // Delete the employee and employee schedule entries from the DataSet. database.Tables["Employees"].Rows[index].Delete(); database.Tables["Employee Schedule"].Rows[index].Delete(); // Commit changes to the DataSet. database.AcceptChanges(); } else if (table == "Customers") { // Remove the customer and corresponding customer attendance entry from the database. DatabaseWorker.RemoveCustomer(Convert.ToInt32(drv[0])); // Get the grid customer entry. Record record = gnt.ChildTable.ParentTable.CurrentElement.GetRecord(); // Get the index of the customer entry. // This index is the same as the index of the corresponding customer attendance entry. int index = databaseGrid.GetTable("Customers").UnsortedRecords.IndexOf(record); // Delete the corresponding customer attendance entry from the grid. databaseGrid.GetTable("Customer Attendance").UnsortedRecords[index].Delete(); // Delete the customer entry from the grid. record.Delete(); // Delete the customer and customer attendance entries from the DataSet. database.Tables["Customers"].Rows[index].Delete(); database.Tables["Customer Attendance"].Rows[index].Delete(); // Commit changes to the DataSet. database.AcceptChanges(); } // If the table is neither of the above, display the appropriate error message. else if (table == "Employee Schedule") { MessageBoxAdv.Show(this, "Cannot remove employee schedule entry directly. \n" + "To remove it, delete the corresponding employee entry.", "Error"); } else if (table == "Customer Attendance") { MessageBoxAdv.Show(this, "Cannot remove customer attendance entry directly. \n" + "To remove it, delete the corresponding customer entry.", "Error"); } } catch (Exception) { } }
/// <summary> /// **************** TODO: COMPLETE DOCUMENTATION ***************** /// /// When the "Remove Entry" button is clicked, remove the selected employee and customer entries /// along with the related schedule and attendance entries respectively, from the database and from the /// DataSet. Update the grid to the reflect these changes. /// </summary> private void removeButton_Click(object sender, EventArgs e) { try { GridRangeInfoList range = databaseGrid.TableModel.Selections.GetSelectedRows(true, true); foreach (GridRangeInfo info in range) { Element element = databaseGrid.TableModel.GetDisplayElementAt(info.Top); GridNestedTable gnt = element as GridNestedTable; GridNestedTable gnt1 = gnt; while (gnt1 != null && gnt1.ChildTable != null) { gnt = gnt1; gnt1 = gnt.ChildTable.ParentTable.CurrentElement as GridNestedTable; } DataRowView drv = gnt.ChildTable.ParentTable.CurrentElement.GetData() as DataRowView; string table = gnt.ChildTable.ParentTable.CurrentElement.GetRecord().ParentChildTable.Name; if (table == "Employees") { DatabaseWorker.RemoveEmployee(Convert.ToInt32(drv[0])); Record record = gnt.ChildTable.ParentTable.CurrentElement.GetRecord(); int index = databaseGrid.GetTable("Employees").UnsortedRecords.IndexOf(record); databaseGrid.GetTable("Employee Schedule").UnsortedRecords[index].Delete(); record.Delete(); database.Tables["Employees"].Rows[index].Delete(); database.Tables["Employee Schedule"].Rows[index].Delete(); database.AcceptChanges(); } else if (table == "Customers") { DatabaseWorker.RemoveCustomer(Convert.ToInt32(drv[0])); Record record = gnt.ChildTable.ParentTable.CurrentElement.GetRecord(); int index = databaseGrid.GetTable("Customers").UnsortedRecords.IndexOf(record); databaseGrid.GetTable("Customer Attendance").UnsortedRecords[index].Delete(); record.Delete(); database.Tables["Customers"].Rows[index].Delete(); database.Tables["Customer Attendance"].Rows[index].Delete(); database.AcceptChanges(); } else if (table == "Employee Schedule") { MessageBoxAdv.Show(this, "Cannot remove employee schedule entry directly. \n" + "To remove it, delete the corresponding employee entry.", "Error"); } else if (table == "Customer Attendance") { MessageBoxAdv.Show(this, "Cannot remove customer attendance entry directly. \n" + "To remove it, delete the corresponding customer entry.", "Error"); } } } catch (Exception) { MessageBoxAdv.Show(this, "Not a valid entry", "Error"); } }