private void ButtonSave_Click(object sender, EventArgs e) { if (DataGridViewTimes.SelectedRows.Count > 0) { // vars string timeStampId = DataGridViewTimes.SelectedRows[0].Cells[2].Value.ToString(); try { //Make sure clockOut date is the same as clockIn date clockInTimePicker.Value = datePicker.Value.Date + clockInTimePicker.Value.TimeOfDay; clockOutTimePicker.Value = datePicker.Value.Date + clockOutTimePicker.Value.TimeOfDay; Dictionary <String, String> data = new Dictionary <String, String>(); data.Add("clockIn", clockInTimePicker.Value.ToString(StringFormats.sqlTimeFormat)); data.Add("clockOut", clockOutTimePicker.Value.ToString(StringFormats.sqlTimeFormat)); if (sql.Update("timeStamps", data, String.Format("timeStamps.id = {0}", timeStampId))) { DataGridViewTimes.SelectedRows[0].Cells[0].Value = clockInTimePicker.Value.ToString(StringFormats.timeStampFormat); DataGridViewTimes.SelectedRows[0].Cells[1].Value = clockOutTimePicker.Value.ToString(StringFormats.timeStampFormat); DataGridViewTimes.Sort(DataGridViewTimes.Columns[0], System.ComponentModel.ListSortDirection.Ascending); } // disable duplicate saving DisableUI(); DisableTimePickers(); } catch (Exception err) { MessageBox.Show(this, "There was an error while trying to save your changes.\n\n" + err.Message, "Time Save Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void ButtonReset_Click(object sender, EventArgs e) { // make sure nothing needs to be saved if (ButtonSave.Enabled) { // switch on user choice switch (MessageBox.Show(this, "There are unsaved changes. Would you like to save them before resetting this window?", "Unsaved Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk)) { case DialogResult.Yes: ButtonSave_Click(sender, e); break; case DialogResult.Cancel: return; } } // reset gui TextBoxCardID.Clear(); TextBoxCardID.Enabled = true; ComboBoxMonth.SelectedIndex = DateTime.Today.Month - 1; ComboBoxMonth.Enabled = true; NumericUpDownYear.Value = (decimal)DateTime.Today.Year; NumericUpDownYear.Enabled = true; DataGridViewTimes.ClearSelection(); DataGridViewTimes.RowStateChanged -= new System.Windows.Forms.DataGridViewRowStateChangedEventHandler(DataGridViewTimes_RowStateChanged); DataGridViewTimes.DataSource = null; DisableUI(); DisableTimePickers(); ButtonLoad.Enabled = true; }
private void DisableUI() { DataGridViewTimes.ClearSelection(); clockInTimePicker.Enabled = false; clockOutTimePicker.Enabled = false; datePicker.Enabled = false; ButtonSave.Enabled = false; DeleteButton.Enabled = false; }
private void ButtonLoad_Click(object sender, EventArgs e) { if (TextBoxCardID.Text != "") { // change gui ButtonLoad.Text = "Loading Times..."; ButtonLoad.Enabled = false; TextBoxCardID.Enabled = false; ComboBoxMonth.Enabled = false; NumericUpDownYear.Enabled = false; DataGridViewTimes.Rows.Clear(); // get month beginning and end DateTime monthStart = new DateTime((int)NumericUpDownYear.Value, ComboBoxMonth.SelectedIndex + 1, 1); DateTime monthEnd = monthStart.AddMonths(1); // check if this is the card we're looking for if (Helper.EmployeeExists(TextBoxCardID.Text, sql)) { try { dt = sql.GetDataTable("select strftime('%m/%d/%Y %H:%M:%S', clockIn) as clockIn," + " strftime('%m/%d/%Y %H:%M:%S', clockOut) as clockOut, id " + " from timeStamps where employeeID=" + TextBoxCardID.Text.Trim() + " and clockOut<>'' " + " and cast(strftime('%m', clockIn) as integer) = " + (int)(ComboBoxMonth.SelectedIndex + 1) + " and cast(strftime('%Y', clockIn) as integer) = " + (int)NumericUpDownYear.Value + ";"); if (dt.Rows.Count > 0) { DataGridViewTimes.DataSource = dt; foreach (DataGridViewRow entry in DataGridViewTimes.Rows) { entry.Cells[0].Value = DateTime.Parse(entry.Cells[0].Value.ToString()).ToString(StringFormats.timeStampFormat); entry.Cells[1].Value = DateTime.Parse(entry.Cells[1].Value.ToString()).ToString(StringFormats.timeStampFormat); } DataGridViewTimes.Sort(DataGridViewTimes.Columns[0], System.ComponentModel.ListSortDirection.Ascending); DataGridViewTimes.ClearSelection(); //Hide id column, used to determine which entry to update/remove DataGridViewTimes.Columns[2].Visible = false; DataGridViewTimes.RowStateChanged += new System.Windows.Forms.DataGridViewRowStateChangedEventHandler(DataGridViewTimes_RowStateChanged); } else { // notify user no times for specified month MessageBox.Show(this, "It looks like there isn't any time you logged in the selected month. Are you sure the year and month selections are correct?", "No Times", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); // reset gui ResetUI(); } } catch (Exception err) { MessageBox.Show(this, "There was an error while trying to load your clocked times.\n\n" + err.Message, "Times Load Error", MessageBoxButtons.OK, MessageBoxIcon.Error); DataGridViewTimes.DataSource = null; // enable controls TextBoxCardID.Enabled = true; ComboBoxMonth.Enabled = true; NumericUpDownYear.Enabled = true; ButtonLoad.Enabled = true; ButtonSave.Enabled = false; DeleteButton.Enabled = false; } } else { MessageBox.Show(this, "The card you entered wasn't found. Are you sure you typed it in correctly?", "Card Not Found!", MessageBoxButtons.OK, MessageBoxIcon.Error); // reset gui ResetUI(); } // change 'status' ButtonLoad.Text = "Load Times"; } }