/// <summary> /// When the "Add Employee" button is clicked, prompt the user to enter the information of the new /// employee, and then add the values to the database. Update the grid after the employee was added. /// </summary> private void addEmployeeButton_Click(object sender, EventArgs e) { // Create and display an "Add Employee" form for the user to enter employee information. // This form will also insert the new entries to the database. AddEmployeeForm prompt = new AddEmployeeForm(); prompt.ShowDialog(); // If the form was fully filled in and the entry was successfully inserted in the database, // create new rows and update the grid. if (prompt.DialogResult == DialogResult.OK) { // Hold the employee's basic information and schedule values that are retrieved from the database, // from the newly added employee entries. string[] employeeData = DatabaseWorker.GetEmployeeEntry(DatabaseWorker.tempId); string[] scheduleData = DatabaseWorker.GetEmployeeScheduleEntry(DatabaseWorker.tempId); // Create new rows that will hold the newly added employee's basic information // and schedule values. DataRow employeeEntry = database.Tables["Employees"].NewRow(); DataRow employeeScheduleEntry = database.Tables["Employee Schedule"].NewRow(); // Populate each column of the new Employee and Employe Schedule rows with data from the database. for (int i = 0; i < database.Tables["Employees"].Columns.Count; i++) { employeeEntry[i] = employeeData[i]; } for (int i = 0; i < database.Tables["Employee Schedule"].Columns.Count; i++) { employeeScheduleEntry[i] = scheduleData[i]; } // Add the newly created Employee and Employee Schedule rows to the DataSet database.Tables["Employees"].Rows.Add(employeeEntry); database.Tables["Employee Schedule"].Rows.Add(employeeScheduleEntry); } }
/// <summary> /// **************** TODO: COMPLETE DOCUMENTATION ***************** /// /// Whenever the value of a cell is edited, update the database entry and the corresponding DataSet value. /// Updat the grid to reflect these changes. /// </summary> private void databaseGrid_RecordValueChanged(object sender, RecordValueChangedEventArgs e) { // Object that holds the values of the record that the edited cell belongs to. DataRowView drv = e.Record.GetData() as DataRowView; // If the cell's record belongs to the Employee table if (e.Record.ParentChildTable.Name == "Employees") { ThreadPool.QueueUserWorkItem(state => { DatabaseWorker.EditEmployee(Convert.ToInt32(drv[0]), drv[1].ToString(), drv[2].ToString(), drv[3].ToString(), drv[4].ToString()); string[] updatedEmployeeValues = DatabaseWorker.GetEmployeeEntryNoId(Convert.ToInt32(drv[0])); int index = databaseGrid.GetTable("Employees").UnsortedRecords.IndexOf(e.Record); e.Record.UpdateValues(updatedEmployeeValues); databaseGrid.GetTable("Employee Schedule").UnsortedRecords[index].SetValue("Name", drv[1].ToString()); } ); } // If the cell's record belongs to the Employee Schedule table else if (e.Record.ParentChildTable.Name == "Employee Schedule") { string[] scheduleValues = new string[7]; for (int i = 2; i < 9; i++) { scheduleValues[i - 2] = drv[i].ToString(); } ThreadPool.QueueUserWorkItem(state => { DatabaseWorker.EditEmployeeSchedule(Convert.ToInt32(drv[0]), scheduleValues); string[] updatedScheduleValues = DatabaseWorker.GetEmployeeScheduleEntry(Convert.ToInt32(drv[0])); e.Record.UpdateValues(updatedScheduleValues); } ); } // If the cell's record belongs to the Customer table else if (e.Record.ParentChildTable.Name == "Customers") { ThreadPool.QueueUserWorkItem(state => { DatabaseWorker.EditCustomer(Convert.ToInt32(drv[0]), drv[1].ToString(), drv[2].ToString(), drv[3].ToString()); string[] updatedCustomerValues = DatabaseWorker.GetCustomerEntryNoId(Convert.ToInt32(drv[0])); int index = databaseGrid.GetTable("Customers").UnsortedRecords.IndexOf(e.Record); e.Record.UpdateValues(updatedCustomerValues); databaseGrid.GetTable("Customer Attendance").UnsortedRecords[index].SetValue("Name", drv[1].ToString()); } ); } // If the cell's record belongs the Customer Attendance table. else if (e.Record.ParentChildTable.Name == "Customer Attendance") { string[] attendanceValues = new string[7]; for (int i = 2; i < 9; i++) { attendanceValues[i - 2] = drv[i].ToString(); } ThreadPool.QueueUserWorkItem(state => { DatabaseWorker.EditCustomerAttendance(Convert.ToInt32(drv[0]), attendanceValues); string[] updatedAttendanceValues = DatabaseWorker.GetCustomerAttendanceEntry(Convert.ToInt32(drv[0])); e.Record.UpdateValues(updatedAttendanceValues); } ); } }