Ejemplo n.º 1
0
        /// <summary>
        /// When the "Add Customer" button is clicked, prompt the user to enter the information of the new
        /// customer, and then add the values to the database. Update the grid after the customer was added.
        /// </summary>
        private void addCustomerButton_Click(object sender, EventArgs e)
        {
            // Create and display an "Add Customer" form for the user to enter customer information.
            // This form will also insert the new entries to the database.
            AddCustomerForm prompt = new AddCustomerForm();

            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 customer's basic information and attendance values that are retrieved from the database,
                // from the newly added customer entries.
                string[] customerData           = DatabaseWorker.GetCustomerEntry(DatabaseWorker.tempId);
                string[] customerAttendanceData = DatabaseWorker.GetCustomerAttendanceEntry(DatabaseWorker.tempId);

                // Create new rows that will hold the newly added customer's basic information
                // and attendance values.
                DataRow customerEntry           = database.Tables["Customers"].NewRow();
                DataRow customerAttendanceEntry = database.Tables["Customer Attendance"].NewRow();

                // Populate each column of the new Customer and Customer Attendance rows with data from the database.
                for (int i = 0; i < database.Tables["Customers"].Columns.Count; i++)
                {
                    customerEntry[i] = customerData[i];
                }

                for (int i = 0; i < database.Tables["Customer Attendance"].Columns.Count; i++)
                {
                    customerAttendanceEntry[i] = customerAttendanceData[i];
                }

                // Add the newly created Customer and Customer Attendance rows to the DataSet
                database.Tables["Customers"].Rows.Add(customerEntry);
                database.Tables["Customer Attendance"].Rows.Add(customerAttendanceEntry);
            }
        }
Ejemplo n.º 2
0
        /// <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);
                }
                                             );
            }
        }