Bug: NullReferenceException raised by IGridViewEditor.SetText in DateTimePickerGridViewEditor when typing a key into a GridViewDataColumn that contains a collapsed RadDateTimePicker

1 Answer 4 Views
DateTimePicker GridView
Stephen
Top achievements
Rank 1
Stephen asked on 05 Aug 2025, 04:58 PM

Is this the best place to report bugs?

This is with 2024.4.1213.462.

To duplicate the issue:

  1. With VS 2022, create a WPF (.NET Framework) application,
  2. Delete MainWindow.xaml and MainWindow.xaml.cs
  3. Unzip the attached file, place MainWindow.xaml and MainWindow.xaml.cs from the zip where the old files were or use the pasted text below
  4. Build and run the application
  5. Put focus on a cell in a row in the grid and type a letter

Expect the following exception to be raised

NameValueType
$exception{"Object reference not set to an instance of an object."}System.NullReferenceException

at Telerik.Windows.Controls.DateTimePickerGridViewEditor.Telerik.Windows.Controls.GridView.IGridViewEditor.SetText(String text) in Telerik.Windows.Controls\DateTimePickerGridViewEditor.cs:line 56

 

In case the zip doesn't work:

MainWindow.xaml.cs:

namespace WpfApp14
{
    using System;
    using System.ComponentModel;
    using System.Windows;
    using System.Collections.ObjectModel;

    public sealed class GridValueViewModel : INotifyPropertyChanged
    {
        private string someValue = string.Empty;

        public GridValueViewModel(string someValue)
        {
            this.SomeValue = someValue;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public string SomeValue
        {
            get
            {
                return this.someValue;
            }

            set
            {
                value = value ?? string.Empty;
                if (this.SomeValue != value)
                {
                    this.someValue = value;
                    this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(this.SomeValue)));
                }
            }
        }

        public DateTime? DateValue
        {
            get
            {
                if (DateTime.TryParse(this.SomeValue, out var value))
                {
                    return value;
                }

                return null;
            }

            set
            {
                if (value == null)
                {
                    this.SomeValue = string.Empty;
                    return;
                }

                this.SomeValue = value.ToString();
            }
        }

        public ObservableCollection<string> SomeValueItems { get; } = new ObservableCollection<string>(
            new[]
            {
                "cat",
                "dog",
                "bird"
            });
        
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
            this.DataContext = this;
        }

        public ObservableCollection<GridValueViewModel> Values { get; } = new ObservableCollection<GridValueViewModel>(
            new[]
            {
                new GridValueViewModel("cat"),
                new GridValueViewModel("dog"),
                new GridValueViewModel("bird")
            });
    }
}

 

MainWindow.xaml:

<Window x:Class="WpfApp14.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <telerik:RadGridView
        AutoGenerateColumns="False"
        ItemsSource="{Binding Values, Mode=OneWay}">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn
                Header="Column 1">
                <telerik:GridViewDataColumn.CellTemplate>
                    <DataTemplate>
                        <Label
                            Content="{Binding SomeValue}" />
                    </DataTemplate>
                </telerik:GridViewDataColumn.CellTemplate>
                <telerik:GridViewDataColumn.CellEditTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition
                                    Width="*" />
                            </Grid.ColumnDefinitions>
                            <TextBox
                                Grid.Column="0"
                                Text="{Binding SomeValue, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                                Visibility="Collapsed" />
                            <telerik:RadComboBox
                                Grid.Column="0"
                                HorizontalAlignment="Stretch"
                                IsEditable="False"
                                ItemsSource="{Binding SomeValueItems}"
                                SelectedItem="{Binding SomeValue}" />
                            <telerik:RadDateTimePicker
                                Grid.Column="0"
                                HorizontalAlignment="Stretch"
                                InputMode="DatePicker"
                                DateTimeWatermarkContent="{x:Null}"
                                SelectedValue="{Binding DateValue, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                                Visibility="Collapsed" />
                        </Grid>
                    </DataTemplate>
                </telerik:GridViewDataColumn.CellEditTemplate>
            </telerik:GridViewDataColumn>
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>
</Window>

Stephen
Top achievements
Rank 1
commented on 05 Aug 2025, 05:01 PM

Maybe the dateTimePicker member field of the DateTimePickerGridViewEditor object is null when the SetText method is called.

1 Answer, 1 is accepted

Sort by
0
Stenly
Telerik team
answered on 07 Aug 2025, 01:58 PM

Hello Stephen,

Thank you for the provided information.

Generally, when requesting a feature or reporting a bug, you can use our feedback portal, which can be found at the following link:

Progress® Telerik® UI for WPF Feedback Portal

About the reported scenario, it seems that with this setup, when a cell goes into edit mode by typing on the keyboard, the RadGridView control will retrieve the first editing element, which has a GridViewEditorProviderAttribute. The returned object is of the type of DateTimePickerGridViewEditor, which will operate with the RadDateTimePicker element that is defined in the CellEditTemplate of the column. Then, the internal logic will check whether the focus is inside the input box (the RadWatermarkTextBox element used for input in the RadDateTimePicker element). This element is cached to a property, which is assigned a value on the OnApplyTemplate method of RadDateTimePicker. However, this method will not be called when the Visibility property is set to Collapsed, as the element will not be measured and arranged during the layout, thus resulting in the reported NullReferenceException.

With this in mind, may I ask if it would be possible to share a bit more information about this setup? For example, why are both the TextBox and RadDateTimePicker elements not displayed in the CellEditTemplate property? Is the visibility of these elements going to be changed based on an application logic on your end? If this were the scenario, the suggested approach would be to use the CellEditTemplateSelector property and return different DataTemplates for editing the cells, based on your application's criteria.

More information about the CellEditTemplateSelector can be found in the following article:

WPF DataGrid - CellTemplateSelector and CellEditTemplateSelector - Telerik UI for WPF

With this being said, I will be awaiting your reply.

Regards,
Stenly
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Stephen
Top achievements
Rank 1
commented on 07 Aug 2025, 02:34 PM

"Is the visibility of these elements going to be changed based on an application logic on your end?"

The visibility is based on the user's choice in a separate column - see the "Definable field type 01" column.


Stenly
Telerik team
commented on 07 Aug 2025, 02:47 PM

Hello Stephen,

If this is the case, what I could suggest would be to use the CellEditTemplateSelector functionality and return a different DataTemplate based on the choice made by the other column.

Could you give this suggestion a try?

Tags
DateTimePicker GridView
Asked by
Stephen
Top achievements
Rank 1
Answers by
Stenly
Telerik team
Share this question
or