// event handler for selected cell chnage datagrid private void dataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { if (dataGrid.Items.IndexOf(dataGrid.CurrentItem) < 5) { if (chooseCourse.SelectedItem != null) { if (dataGrid.CurrentCell.Column != null) { int columnIndex = dataGrid.CurrentCell.Column.DisplayIndex; int rowIndex = dataGrid.Items.IndexOf(dataGrid.CurrentItem); DataRowView v = (DataRowView)dataGrid.Items[rowIndex]; string day = (string)v[0]; string timeString = (string)dataGrid.Columns[columnIndex].Header; string cName = chooseCourse.Text; string cId = chooseCourse.SelectedValue.ToString(); string day2; string time; // loops through the datatable foreach (DataRow dr in dt.Rows) { foreach (DataColumn dc in dt.Columns) { day2 = dr[0].ToString(); time = dc.ColumnName.ToString(); // the datatable day and datatable time is equal and the is no data in the cell if ((day2 == day) && (time == timeString) && (dr[dc].ToString() == "")) { // open insert timeslot window insertTimetable it = new insertTimetable(day, timeString, cId, cName); it.Owner = this; it.ShowDialog(); // refresh datagrid after timeslot insert refreshDatagrid(); } // esle if the datacell contians data else if ((day2 == day) && (time == timeString) && (dr[dc].ToString() != "") && (dr[dc].ToString() != "Monday") && (dr[dc].ToString() != "Tuesday") && (dr[dc].ToString() != "Wednesday") && (dr[dc].ToString() != "Thursday") && (dr[dc].ToString() != "Friday")) { // load edit timeslot window editTimetable editTtable = new editTimetable(day, timeString, cId, cName); editTtable.Owner = this; editTtable.ShowDialog(); // refresh datagrid after edited timeslot refreshDatagrid(); } } } } } else { MessageBox.Show("Must choose course"); } } }
private void dataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e) { if (dataGrid.Items.IndexOf(dataGrid.CurrentItem) < 5) { if (chooseCourse.SelectedItem != null) { if (dataGrid.CurrentCell.Column != null) { int columnIndex = dataGrid.CurrentCell.Column.DisplayIndex; int rowIndex = dataGrid.Items.IndexOf(dataGrid.CurrentItem); DataRowView v = (DataRowView)dataGrid.Items[rowIndex]; string day = (string)v[0]; string timeString = (string)dataGrid.Columns[columnIndex].Header; string cName = chooseCourse.Text; //MessageBox.Show((string)v.ToString()); //MessageBox.Show(day + " and " + timeString); string cId = chooseCourse.SelectedValue.ToString(); string day2; string time; foreach (DataRow dr in dt.Rows) { foreach (DataColumn dc in dt.Columns) { day2 = dr[0].ToString(); time = dc.ColumnName.ToString(); if ((day2 == day) && (time == timeString) && (dr[dc].ToString() == "")) { insertTimetable it = new insertTimetable(day, timeString, cId, cName); it.ShowDialog(); //MessageBox.Show(day + " time: " + time); string query = "SELECT m.moduleName, r.roomCode, l.lecturerName FROM Timetable t, Module m, Room r, Lecturer l WHERE t.moduleId = m.moduleId AND t.roomId = r.roomId AND t.lecturerId = l.lecturerId AND courseId = @courseId AND day = @day AND time = @time;"; string mName; string rCode; string lName; //string mName; //string rCode; using (var conn = new SqlConnection(dbConnectionString)) using (var cmd = new SqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@courseId", chooseCourse.SelectedValue.ToString()); cmd.Parameters.AddWithValue("@day", day); cmd.Parameters.AddWithValue("@time", time); conn.Open(); using (var reader = cmd.ExecuteReader()) { if (reader.Read()) { mName = reader.GetString(reader.GetOrdinal("moduleName")); rCode = reader.GetString(reader.GetOrdinal("roomCode")); lName = reader.GetString(reader.GetOrdinal("lecturerName")); //line below inserts into cell dr[dc] = " Module: " + mName + "\n Room: " + rCode + "\n Lecturer: " + lName; } } } } else { //add dialog box to allow user to edit, maybe try drag and drop } } } } } else { MessageBox.Show("Must choose course"); } } }