How to load null value to the GridDateTimeColumn when AllowInlineEditing is set to true in WPF DataGrid?
The SfDatePicker is loaded as UIElement of the GridDateTimeColumn in edit mode. By default, old value is replaced when you try to commit the blank value on editing and when the SfDatePicker.AllowInlineEditing property is enabled.
You can overcome this by deriving a new class from the GridCellDateTimeRenderer and overriding the OnEditElementLoaded virtual method. In this method, you can get the corresponding editor, SfDatePicker of the GridDateTimeColumn and you can explicitly set the SfDatePicker.Value as null in SfTextBoxExt.TextChanged event.
The SfTextBoxExt is loaded as editor of the SfDatePicker control. |
C#
public class GridCellDateTimeRendererExt : GridCellDateTimeRenderer { SfDatePicker datepicker = null; protected override void OnEditElementLoaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) { datepicker = (SfDatePicker)sender; //Gets the SfTextBoxExt editor loaded in the SfDatePicker SfTextBoxExt textBox = ((SfDatePicker)sender).FindDescendantByName("PART_TextBoxExt") as SfTextBoxExt; //TextChanged event of SfTextBoxExt is wired textBox.TextChanged += textBox_TextChanged; base.OnEditElementLoaded(sender, e); } void textBox_TextChanged(object sender, Windows.UI.Xaml.Controls.TextChangedEventArgs e) { var text = sender as SfTextBoxExt; if (text.Text == string.Empty && datepicker.AllowInlineEditing) { //Sets null to value of SfDatePicker expilicitly datepicker.SetValue(SfDatePicker.ValueProperty, null); } } } | |
In the above code example, FindDescendantByName is an extension helper method available in a attached sample. | |
Refer to the following code example to remove the default GridCellDateTimeRenderer and add the customized GridCellDateTimeRendererExt to the SfDataGrid.CellRenderer collection.
C#
public MainPage() { this.InitializeComponent(); //Customized renderer is added to CellRenderers collection this.grid.CellRenderers.Remove("DateTime"); this.grid.CellRenderers.Add("DateTime", new GridCellDateTimeRendererExt()); }
Sample Links:
WinRT: https://www.syncfusion.com/downloads/support/directtrac/141421/ze/WinRT2074283480
Conclusion
I hope you enjoyed learning about how to load null value to the GridDateTimeColumn when AllowInlineEditing is set to true in WPF DataGrid.
You can refer to our WPF Grid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF Grid documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!