コード例 #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);
            }
        }