/// <summary> /// Find the row and column index of a clicked cell in a datagrid /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DataGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { DependencyObject dep = (DependencyObject)e.OriginalSource; while (dep != null && !(dep is DataGridCell)) { dep = VisualTreeHelper.GetParent(dep); Console.WriteLine(dep); } if (dep == null) { return; } if (dep is DataGridCell) { DataGridCell cell = dep as DataGridCell; // navigate further up the tree to get the row while ((dep != null) && !(dep is DataGridRow)) { dep = VisualTreeHelper.GetParent(dep); } DataGridRow row = dep as DataGridRow; //TODO: these are the row/column indices //use these to update the collection with a new time range provided through a dialog box Console.WriteLine("Row: {0} Column: {1}", row.GetIndex(), cell.Column.DisplayIndex); //check if clicked cell was staff if (!(cell.Column.DisplayIndex == 0)) { //after row and column is found load up dialog to input new start and end times DayScheduleDialog dsd = new DayScheduleDialog(); DayScheduleVM dsvm = new DayScheduleVM(); //populate vm with currently selected cell dsvm.EmployeeIndex = row.GetIndex(); dsvm.DayIndex = cell.Column.DisplayIndex; dsd.DataContext = dsvm; if (dsd.ShowDialog() == true) { Console.WriteLine("{0} to {1}", dsvm.StartTime, dsvm.EndTime); //add new schedule to database vm.AddDaySchedule(dsvm); vm.RefreshDataGrid(); } } } }
public void AddDaySchedule(DayScheduleVM vm) { // Console.WriteLine("{0}, {1} to {2} on day {3}", EmpList.ElementAt(vm.EmployeeIndex).Name, vm.StartTime, vm.EndTime, WeekDates.ElementAt(vm.DayIndex - 1)); var currentDay = WeekDates.ElementAt(vm.DayIndex - 1); var currentEmployee = EmployeeSchedules.ElementAt(vm.EmployeeIndex).employee; Schedule schdl = new Schedule(currentDay.Month, currentDay.Day, currentDay.Year, vm.StartTime, vm.EndTime); //get new schedule id from database var scheduleId = ScheduleTableDB.AddSchedule(schdl); //add employee id and schedule id to linking table EmployeeScheduleTableDB.AddEmployeeSchedule(currentEmployee.EmployeeId, scheduleId); }