Ejemplo n.º 1
0
        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");
                }
            }
        }