Serge Boulet

Serge Boulet

  • NA
  • 3
  • 887

Issue with Date Picker needed to be used twice with Begin Edit

Mar 28 2021 4:08 AM
This is a WPF application, using a Data Grid with Date Picker.
 
The goal is to click on the calendar, select a date, then the Data Grid goes in edit mode so as soon as I click on another cell, the Update is done on the database and the row is added on the grid, that part is working.
 
But, when I initially click on the Date Picker, select the date, because I do the Data Grid Begin Edit, the date selected is never displayed in the control, it goes in Edit mode, at this point I can still click on another cell to have the Insert in the database occur and the row added to the Data Grid, but the Date is inserted as null (I can also select the date again on the Date Picker and click on another cell to have the date added to database and the added row in the Data Grid).
 
Is there a way to change this behavior so the Date chosen is kept and it still goes in edit mode without me having to click again on the Date Picker, select the date again and then click on another cell for the Insert to happen?
 
May you help me out with this please?
 
Here is the code for the column creation (using a Template):
  1. templateColumn = new DataGridTemplateColumn();  
  2. templateColumn.Header = "Testing date";  
  3. FrameworkElementFactory datePickerFactoryElem = new FrameworkElementFactory(typeof(DatePicker));  
  4. templateColumnBinding = new Binding("date_time");  
  5. templateColumnBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;  
  6. templateColumnBinding.Mode = BindingMode.TwoWay;  
  7. datePickerFactoryElem.AddHandler(DatePicker.SelectedDateChangedEvent, new EventHandler(RowEditEnding));  
  8. datePickerFactoryElem.SetValue(DatePicker.SelectedDateProperty, templateColumnBinding);  
  9. datePickerFactoryElem.SetValue(DatePicker.DisplayDateProperty, templateColumnBinding);  
  10. cellTemplate.VisualTree = datePickerFactoryElem;  
  11. templateColumn.CellTemplate = cellTemplate;  
  12. DataGrid.Columns.Add(templateColumn);  
Here is the code to begin the edit when I select a Date with the Date Picker:
  1. private void RowEditEnding(object sender, SelectionChangedEventArgs e)  
  2. {  
  3. DataGrid.BeginEdit();  
  4. }  
Here is the code fired when I click on another cell:
  1. private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)  
  2. {  
  3. if (!isManualEditCommit)  
  4. {  
  5. isManualEditCommit = true;  
  6. DataGrid grid = (DataGrid)sender;  
  7. grid.CommitEdit(DataGridEditingUnit.Row, true);  
  8. isManualEditCommit = false;  
  9. }  
  10. }  
  11. private void DataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)  
  12. {  
  13. DataRowView rowView = e.Row.Item as DataRowView;  
  14. rowBeingEdited_DataGrid = rowView;  
  15. }  
  16. private void DataGrid_CurrentCellChanged(object sender, EventArgs e)  
  17. {  
  18. if (rowBeingEdited_DataGrid != null)  
  19. {  
  20. DataGrid.CommitEdit();  
  21. conn = new MySqlConnection(connStr);  
  22. MySqlCommand cmdInsert;  
  23. MySqlCommand cmdUpdate;  
  24. cmdUpdate = new MySqlCommand(@"UPDATE…  
  25. SET…  
  26. WHERE…;", conn);  
  27. cmdUpdate.Parameters.Add("…");  
  28. cmdUpdate.Parameters.Add("…");  
  29. cmdInsert = new MySqlCommand(@"INSERT INTO…  
  30. (…,  
  31. …,)  
  32. VALUES  
  33. …,  
  34. …);", conn);  
  35. cmdInsert.Parameters.Add("…");  
  36. cmdInsert.Parameters.Add("…");  
  37. mySqlDataAdapter.InsertCommand = cmdInsert;  
  38. mySqlDataAdapter.UpdateCommand = cmdUpdate;  
  39. mySqlDataAdapter.Update(dataSet);  
  40. conn.Close();  
  41. conn.Dispose();  
  42. DataGrid.ItemsSource = null;  
  43. dataSet.Reset();  
  44. rowBeingEdited_DataGrid.EndEdit();  
  45. rowBeingEdited_DataGrid = null;  
  46. conn = new MySqlConnection(connStr);  
  47. mySqlDataAdapter = new MySqlDataAdapter(@"select * from …", conn);  
  48. command = conn.CreateCommand();  
  49. conn.Open();  
  50. mySqlDataAdapter.Fill(dataSet);  
  51. DataGrid.ItemsSource = dataSet.Tables[0].DefaultView;  
  52. conn.Close();  
  53. conn.Dispose();  
  54. }  
  55. }  
Thank you for your time and help, it is greatly appreciated.
 
Note: Variables names and controls are named better in the original code, I tried to remove as much irrelevant information so the code would be easier to read.