Ejemplo n.º 1
0
        /// <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);
            }
        }