//DGV private void dataGridView2_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter && dataGridView2.SelectedCells.Cast <DataGridViewCell>().Any(x => x.ColumnIndex == 1)) { try { if (dataGridView2.Focused) { DateTime d; if (string.IsNullOrEmpty(dataGridView2.CurrentCell.Value.ToString()) == false) { d = Convert.ToDateTime(dataGridView2.CurrentCell.Value); } else { d = DateTime.Today; } setDate f = new setDate(d); f.ShowDialog(); dataGridView2.CurrentCell.Value = f.result.Date.ToString().Substring(0, 10).Replace('/', '-'); dataGridView1.Rows[0].Cells["Date"].Value = f.result.Date.ToString().Substring(0, 10).Replace('/', '-'); e.Handled = true; } else { return; } } catch { return; } } }
//Sessions private void endSessionButton_Click(object sender, EventArgs e) { if (dataGridView2.SelectedCells.Count <= 0) { return; } int rowindex = dataGridView2.SelectedCells[0].RowIndex; if (dataGridView2.Rows[rowindex].Cells["End_Date"].Value.ToString() == "Active Session") { setDate end = new setDate(DateTime.Now); end.set_Message("Enter End Date"); end.ShowDialog(); if (end.values_set == false) { return; } Console.WriteLine(Convert.ToDateTime(dataGridView2.Rows[rowindex].Cells["Begin_Date"].Value.ToString())); Console.WriteLine(end.result); if (Convert.ToDateTime(dataGridView2.Rows[rowindex].Cells["Begin_Date"].Value.ToString()).Date >= end.result.Date) { a.ErrorBox("End date should be greater than start date of the session"); endSessionButton.PerformClick(); return; } dataGridView2.Rows[rowindex].Cells["End_Date"].Value = end.result; dataGridView2.Rows[rowindex].DefaultCellStyle.BackColor = Color.Coral; dataGridView2.Rows[rowindex].DefaultCellStyle.SelectionBackColor = Color.Orange; } }
private void dataGridView1_KeyDown(object sender, KeyEventArgs e) { if (dataGridView1.Enabled == false || dataGridView1.ReadOnly == true) { return; } //called when tab is pressed at last row or tab is pressed while editing last row if (e.KeyCode == Keys.Tab && (dataGridView1.SelectedCells.Cast <DataGridViewCell>().Any(x => x.ColumnIndex == 6) || this.edit_cmd_send == true)) { Console.WriteLine("Inside Editing"); bool edit_cmd_local = this.edit_cmd_send; this.edit_cmd_send = false; int rowindex_tab = dataGridView1.SelectedCells[0].RowIndex; if (edit_cmd_local == true) { rowindex_tab--; } if (dataGridView1.Rows.Count - 1 == rowindex_tab + 1) { DataGridViewRow row = (DataGridViewRow)dataGridView1.Rows[rowindex_tab].Clone(); row.DefaultCellStyle.BackColor = dataGridView1.DefaultCellStyle.BackColor; row.DefaultCellStyle.SelectionBackColor = dataGridView1.DefaultCellStyle.SelectionBackColor; row.ReadOnly = false; dataGridView1.Rows.Add(row); dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[1]; if (c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab, 1) && !c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab + 1, 1)) { dataGridView1.Rows[rowindex_tab + 1].Cells[1].Value = dataGridView1.Rows[rowindex_tab].Cells[1].Value; } if (c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab, 2) && !c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab + 1, 2)) { dataGridView1.Rows[rowindex_tab + 1].Cells[2].Value = (int.Parse(dataGridView1.Rows[rowindex_tab].Cells[2].Value.ToString()) + 1).ToString(); } if (c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab, 3) && !c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab + 1, 3)) { dataGridView1.Rows[rowindex_tab + 1].Cells[3].Value = dataGridView1.Rows[rowindex_tab].Cells[3].Value; } if (c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab, 4) && !c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab + 1, 4)) { dataGridView1.Rows[rowindex_tab + 1].Cells[4].Value = dataGridView1.Rows[rowindex_tab].Cells[4].Value; } if (c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab, 5) && !c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab + 1, 5)) { dataGridView1.Rows[rowindex_tab + 1].Cells[5].Value = dataGridView1.Rows[rowindex_tab].Cells[5].Value; } return; } if (dataGridView1.Rows.Count - 1 == rowindex_tab) { Console.WriteLine("This case"); return; } if (c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab, 1) && !c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab + 1, 1)) { dataGridView1.Rows[rowindex_tab + 1].Cells[1].Value = dataGridView1.Rows[rowindex_tab].Cells[1].Value; } if (c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab, 2) && !c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab + 1, 2)) { dataGridView1.Rows[rowindex_tab + 1].Cells[2].Value = (int.Parse(dataGridView1.Rows[rowindex_tab].Cells[2].Value.ToString()) + 1).ToString(); } if (c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab, 3) && !c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab + 1, 3)) { dataGridView1.Rows[rowindex_tab + 1].Cells[3].Value = dataGridView1.Rows[rowindex_tab].Cells[3].Value; } if (c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab, 4) && !c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab + 1, 4)) { dataGridView1.Rows[rowindex_tab + 1].Cells[4].Value = dataGridView1.Rows[rowindex_tab].Cells[4].Value; } if (c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab, 5) && !c.Cell_Not_NullOrEmpty(this.dataGridView1, rowindex_tab + 1, 5)) { dataGridView1.Rows[rowindex_tab + 1].Cells[5].Value = dataGridView1.Rows[rowindex_tab].Cells[5].Value; } //bindingSource.EndEdit(); dataGridView1.NotifyCurrentCellDirty(true); dataGridView1.EndEdit(); dataGridView1.NotifyCurrentCellDirty(false); SendKeys.Send("{tab}"); if (edit_cmd_local == false) { SendKeys.Send("{tab}"); } } if (e.KeyCode == Keys.Enter && dataGridView1.SelectedCells.Cast <DataGridViewCell>().Any(x => x.ColumnIndex == 1)) { try { if (dataGridView1.Focused && dataGridView1.CurrentCell.ColumnIndex == 1) { DateTime d; if (!(dataGridView1.CurrentCell.Value == null || dataGridView1.CurrentCell.Value == "")) { d = Convert.ToDateTime(dataGridView1.CurrentCell.Value); } else { d = DateTime.Today; } setDate f = new setDate(d); f.setMinMax(dtp.MinDate, dtp.MaxDate); f.ShowDialog(); dataGridView1.CurrentCell.Value = f.result.Date.ToString().Substring(0, 10); e.Handled = true; } else { dtp.Visible = false; } } catch { Console.WriteLine("DTP Exception"); } } if (e.KeyCode == Keys.Enter && (dataGridView1.SelectedCells.Cast <DataGridViewCell>().Any(x => x.ColumnIndex == 3) || dataGridView1.SelectedCells.Cast <DataGridViewCell>().Any(x => x.ColumnIndex == 4) || dataGridView1.SelectedCells.Cast <DataGridViewCell>().Any(x => x.ColumnIndex == 5))) { dataGridView1.BeginEdit(true); ComboBox c = (ComboBox)dataGridView1.EditingControl; if (c != null) { if (c != null) { c.DroppedDown = true; } SendKeys.Send("{down}"); SendKeys.Send("{up}"); } e.Handled = true; } }
private void startSessionButton_Click(object sender, EventArgs e) { setDate start = new setDate(DateTime.Now); start.set_Message("Enter Start Date"); start.ShowDialog(); if (start.values_set == false) { Console.WriteLine("ret"); return; } //check if start date is in between any previous start and end dates int open_session_index = -1; for (int i = 0; i < dataGridView2.Rows.Count; i++) { DateTime s_i = Convert.ToDateTime(dataGridView2.Rows[i].Cells["Begin_Date"].Value.ToString()); DateTime e_i; try { e_i = Convert.ToDateTime(dataGridView2.Rows[i].Cells["End_Date"].Value.ToString()); } catch { open_session_index = i; continue; } if (start.result.Date >= s_i.Date && start.result.Date <= e_i.Date) { a.ErrorBox("Start Date is in between Begin and End Dates of Session ID: " + dataGridView2.Rows[i].Cells["Session_ID"].Value.ToString()); this.startSessionButton.PerformClick(); return; } } Console.WriteLine("Open session index = " + open_session_index); if (open_session_index == -1) { DateTime last_end_date = Convert.ToDateTime(Convert.ToDateTime(dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells["End_Date"].Value.ToString())); if (start.result.Date > last_end_date.Date) { Console.WriteLine("Case 1a"); dataGridView2.Rows.Add("Session Added", start.result, "Active Session"); dataGridView2.Rows[dataGridView2.Rows.Count - 1].DefaultCellStyle.BackColor = Color.LawnGreen; dataGridView2.Rows[dataGridView2.Rows.Count - 1].DefaultCellStyle.SelectionBackColor = Color.Green; return; } else { while (true) { setDate end = new setDate(DateTime.Now); end.set_Message("Enter End Date (Start Date: " + start.result.ToString().Substring(0, 10).Replace('/', '-') + ")"); end.ShowDialog(); if (end.values_set == false) { return; } if (end.result.Date <= start.result.Date) { a.ErrorBox("End Date should greater than start date"); } for (int i = 0; i < dataGridView2.Rows.Count; i++) { DateTime s_this = Convert.ToDateTime(dataGridView2.Rows[i].Cells["Begin_Date"].Value.ToString()); if (s_this.Date >= start.result.Date && s_this.Date <= end.result.Date) { a.ErrorBox("End date should be between entered start date and start date of next session"); break; } else { Console.WriteLine("Case 2c"); dataGridView2.Rows.Add("Session Added", start.result, end.result); this.dataGridView2.Sort(this.dataGridView2.Columns["Begin_Date"], ListSortDirection.Ascending); return; } } } } } else { DateTime last_start_date = Convert.ToDateTime(Convert.ToDateTime(dataGridView2.Rows[open_session_index].Cells["Begin_Date"].Value.ToString())); if (start.result.Date == last_start_date.Date) { Console.WriteLine("Case 2a"); a.ErrorBox("Start Date cannot be equal to the Begin Date of the open session (Session ID: " + dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells["Session_ID"].Value.ToString() + ")"); this.startSessionButton.PerformClick(); return; } else if (start.result.Date > last_start_date.Date) { Console.WriteLine("Case 2b"); a.ErrorBox("Please close the last session before opening a new one after that"); this.startSessionButton.PerformClick(); return; } else { while (true) { setDate end = new setDate(DateTime.Now); end.set_Message("Enter End Date (Start Date: " + start.result.ToString().Substring(0, 10).Replace('/', '-') + ")"); end.ShowDialog(); if (end.values_set == false) { return; } if (end.result.Date <= start.result.Date) { a.ErrorBox("End Date should greater than start date"); } for (int i = 0; i < dataGridView2.Rows.Count; i++) { DateTime s_this = Convert.ToDateTime(dataGridView2.Rows[i].Cells["Begin_Date"].Value.ToString()); if (s_this.Date >= start.result.Date && s_this.Date <= end.result.Date) { a.ErrorBox("End date should be between entered start date and start date of next session"); break; } else { Console.WriteLine("Case 2c"); dataGridView2.Rows.Add("Session Added", start.result, end.result); this.dataGridView2.Sort(this.dataGridView2.Columns["Begin_Date"], ListSortDirection.Ascending); return; } } } } } }