private void SetAddButton() { AvgHoursGridView.ClearSelection(); DeleteButton.Enabled = false; ClearButton.Enabled = false; ClockInTimePicker.Enabled = true; ClockOutTimePicker.Enabled = true; DayOfWeekComboBox.Enabled = true; SaveButton.Enabled = true; SaveButton.Text = "Add"; }
private void LoadButton_Click(object sender, EventArgs e) { if (TextBoxCardID.Text != "") { try { if (Helper.EmployeeExists(TextBoxCardID.Text, sql)) { //Disable UIs TextBoxCardID.Enabled = false; LoadButton.Enabled = false; dt = sql.GetDataTable("select id, dayOfWeek as Day, clockIn, clockOut from avgHours where employeeId=" + TextBoxCardID.Text + " order by dayOfWeek asc;"); if (dt.Rows.Count > 0) { AvgHoursGridView.DataSource = dt; AvgHoursGridView.ClearSelection(); AvgHoursGridView.Columns[0].Visible = false; // hide id column, used to determine which entry to update/remove foreach (DataGridViewColumn column in AvgHoursGridView.Columns) { column.SortMode = DataGridViewColumnSortMode.NotSortable; } AvgHoursGridView.RowStateChanged += new System.Windows.Forms.DataGridViewRowStateChangedEventHandler(AvgHoursGridView_RowStateChanged); } else { MessageBox.Show("No entries were found for this ID,\n use the fields below to add one.", "No entries found!", MessageBoxButtons.OK, MessageBoxIcon.Information); } SetAddButton(); } else { throw new ArgumentException("The ID you entered does not exist, please make sure ID was entered correctly."); } } catch (Exception err) { MessageBox.Show(err.Message, "An Error occured!", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void SaveButton_Click(object sender, EventArgs e) { try { string dayOfWeek = (DayOfWeekComboBox.SelectedIndex + 1).ToString(); string clockIn = ClockInTimePicker.Value.ToString("HH:mm"); string clockOut = ClockOutTimePicker.Value.ToString("HH:mm"); Dictionary <String, String> data = new Dictionary <String, String> { { "dayOfWeek", dayOfWeek }, { "clockIn", clockIn }, { "clockOut", clockOut } }; if (SaveButton.Text == "Save" && AvgHoursGridView.SelectedRows.Count > 0) { string entryId = AvgHoursGridView.SelectedRows[0].Cells[0].Value.ToString(); if (sql.Update("avgHours", data, String.Format("avgHours.id = {0}", entryId))) { AvgHoursGridView.SelectedRows[0].Cells[1].Value = dayOfWeek; AvgHoursGridView.SelectedRows[0].Cells[2].Value = clockIn; AvgHoursGridView.SelectedRows[0].Cells[3].Value = clockOut; SetAddButton(); } } else if (SaveButton.Text == "Add" && AvgHoursGridView.SelectedRows.Count == 0) { // TODO see if count == 0 check is actually necessary data.Add("employeeID", TextBoxCardID.Text.Trim()); if (sql.Insert("avgHours", data)) { DataTable dt = AvgHoursGridView.DataSource as DataTable; if (dt == null) { dt = new DataTable(); dt.Columns.Add("id", typeof(int)); dt.Columns.Add("Day", typeof(string)); dt.Columns.Add("clockIn", typeof(string)); dt.Columns.Add("clockOut", typeof(string)); AvgHoursGridView.DataSource = dt; AvgHoursGridView.Columns[0].Visible = false; // hide ID column } DataRow dr = dt.NewRow(); dr[0] = sql.ExecuteScalar("select seq from sqlite_sequence where name='avgHours';"); dr[1] = dayOfWeek; dr[2] = clockIn; dr[3] = clockOut; // TODO find out why this event gets deregistered and re-registered (and possibly do whatever it does directly to make it less confusing) AvgHoursGridView.RowStateChanged -= new DataGridViewRowStateChangedEventHandler(AvgHoursGridView_RowStateChanged); dt.Rows.Add(dr); dt.AcceptChanges(); AvgHoursGridView.ClearSelection(); AvgHoursGridView.RowStateChanged += new DataGridViewRowStateChangedEventHandler(AvgHoursGridView_RowStateChanged); } } } catch (Exception err) { MessageBox.Show(this, "There was an error while trying to save the entry.\n\n" + err.Message, "Save Avg Hours Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }