Declined
Last Updated: 12 Nov 2019 14:52 by ADMIN
Corey
Created on: 12 Jul 2019 21:39
Category: GridView
Type: Bug Report
0
GridCheckBoxHeaderCellElement Attach errors when GridControl is null

Using a custom control using the Telerik Presentation Framework, I am receiving a NullException inside the GridCheckBoxHeaderCellElement during the GridViewElement's data load.

The custom control construction follows an example I found somewhere (reference is lost to me at this point):

ControlContainingGridView inherits from RadControl

CreateChildItems creates and adds ControlContainingGridViewElement

 

ControlContainingGridViewElement inherits from RadElement

CreateChildItems creates and adds some layout controls AND a GridViewElement

 

public partial class ControlContainingGridView : RadControl {
        public ControlContainingGridView() {
            InitializeComponent();
            this.TextChanged += ControlContainingGridView_TextChanged;
        }

        private void ControlContainingGridView_TextChanged(object sender, EventArgs e) {
            controlContainingGridViewElement.Text = this.Text;
        }

        private ControlContainingGridViewElement controlContainingGridViewElement = null;


        protected override void CreateChildItems(RadElement parent) {
            controlContainingGridViewElement = new ControlContainingGridViewElement();
            this.RootElement.Children.Add(controlContainingGridViewElement);
            base.CreateChildItems(parent);
        }

        public object DataSource {
            get {
                return controlContainingGridViewElement.GridView.Template.DataSource;
            }
            set {
                try {
                    controlContainingGridViewElement.GridView.Template.DataSource = value;
                } catch (Exception ex) {
                    Debug.WriteLine(ex);
                }
            }
        }

        internal void SetupColumns(Action<MasterGridViewTemplate> setupColumns) {
            controlContainingGridViewElement.GridView.Template.AutoGenerateColumns = false;
            setupColumns(controlContainingGridViewElement.GridView.Template);
        }



    }

    public class ControlContainingGridViewElement : RadElement {

        private TextPrimitive textPrimitive = null;
        private RadButtonElement addButton = null;
        private RadButtonElement deleteButton = null;
        private RadGridViewElement gridViewElement = null;

        public RadGridViewElement GridView { get { return gridViewElement; } }

        public bool ShowAddButton { get { return addButton.Visibility == ElementVisibility.Visible; } set { addButton.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed; } }
        public bool ShowDeleteButton { get { return deleteButton.Visibility == ElementVisibility.Visible; } set { deleteButton.Visibility = value ? ElementVisibility.Visible : ElementVisibility.Collapsed; } }

        public string Text { get { return textPrimitive.Text; } set { textPrimitive.Text = value; } }

        protected override void CreateChildElements() {
            DockLayoutPanel dlp = new DockLayoutPanel();
            dlp.StretchHorizontally = true;
            dlp.StretchHorizontally = true;
            dlp.LastChildFill = true;

            DockLayoutPanel dlpBar = new DockLayoutPanel();
            dlpBar.StretchHorizontally = true;
            DockLayoutPanel.SetDock(dlpBar, Dock.Top);


            var imagePrimitive = new ImagePrimitive();
            DockLayoutPanel.SetDock(imagePrimitive, Dock.Left);

            textPrimitive = new TextPrimitive();
            DockLayoutPanel.SetDock(textPrimitive, Dock.Left);

            var slp = new StackLayoutPanel();
            DockLayoutPanel.SetDock(slp, Dock.Right);

            var addButton = new RadButtonElement {
                Text = "Add",
                MaxSize = new Size() { Height = 20 }
            };
            var deleteButton = new RadButtonElement {
                Text = "Del",
                MaxSize = new Size() { Height = 20 }
            };

            slp.Children.Add(addButton);
            slp.Children.Add(deleteButton);


            dlpBar.Children.Add(slp);
            dlpBar.Children.Add(imagePrimitive);
            dlpBar.Children.Add(textPrimitive);



            gridViewElement = new RadGridViewElement {
                StretchHorizontally = true,
                StretchVertically = true
            };
            gridViewElement.Template.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            gridViewElement.Template.AllowAddNewRow = false;
            gridViewElement.Template.AllowDeleteRow = false;
            gridViewElement.Template.AllowDragToGroup = false;
            gridViewElement.ShowGroupPanel = false;
            gridViewElement.ShowGroupPanelScrollbars = false;

            dlp.Children.Add(dlpBar);
            dlp.Children.Add(gridViewElement);



            this.Children.Add(dlp);
            base.CreateChildElements();
        }

    }

 

During Form load, I programmatically add columns and provide a binding source

 

private List<SampleModel> sampleModels = new List<SampleModel>();
        private BindingSource sampleModelsBinding = new BindingSource();

        private void RadForm1_Load(object sender, EventArgs e) {

            sampleModels.Add(new SampleModel() {
                When = DateTime.Parse("1980-04-23"),
                SomeText = "Purple Bananas",
                IsTrue = true
            });
            sampleModels.Add(new SampleModel() {
                When = DateTime.Parse("2002-07-20"),
                SomeText = "Ball & Chain",
                IsTrue = false
            });
            sampleModelsBinding.DataSource = sampleModels;

            controlContainingGridView.SetupColumns((grid) => {
                grid.AddDateTimeColumn(nameof(SampleModel.When), "When");
                grid.AddTextBoxColumn(nameof(SampleModel.SomeText), "Some Text");
                grid.AddCheckBoxColumn(nameof(SampleModel.IsTrue), "Is True");
            });

            controlContainingGridView.DataSource = sampleModelsBinding;

        }

 

Inclusion of the CheckBox column results in the following error:

Exception thrown: 'System.NullReferenceException' in Telerik.WinControls.GridView.dll

System.NullReferenceException: Object reference not set to an instance of an object.
   at Telerik.WinControls.UI.GridCheckBoxHeaderCellElement.Attach(GridViewColumn data, Object context)
   at Telerik.WinControls.UI.BaseVirtualizedContainer`1.InsertElement(Int32 position, IVirtualizedElement`1 element, T data)
   at Telerik.WinControls.UI.BaseVirtualizedContainer`1.UpdateElement(Int32 position, T data)
   at Telerik.WinControls.UI.BaseVirtualizedContainer`1.MeasureElements()
   at Telerik.WinControls.UI.BaseVirtualizedContainer`1.MeasureOverride(SizeF availableSize)
   at Telerik.WinControls.RadElement.MeasureCore(SizeF availableSize)
   at Telerik.WinControls.RadElement.Measure(SizeF availableSize)
   at Telerik.WinControls.UI.GridVirtualizedRowElement.MeasureElements(SizeF availableSize, SizeF clientSize, Padding borderThickness)
   at Telerik.WinControls.UI.LightVisualElement.MeasureOverride(SizeF availableSize)
   at Telerik.WinControls.UI.GridRowElement.MeasureOverride(SizeF availableSize)
   at Telerik.WinControls.RadElement.MeasureCore(SizeF availableSize)
   at Telerik.WinControls.RadElement.Measure(SizeF availableSize)
   at Telerik.WinControls.UI.VirtualizedStackContainer`1.MeasureElementCore(RadElement element, SizeF availableSize)
   at Telerik.WinControls.UI.ScrollableRowsContainerElement.MeasureElementCore(RadElement element, SizeF availableSize)
   at Telerik.WinControls.UI.VirtualizedStackContainer`1.MeasureElement(IVirtualizedElement`1 element)
   at Telerik.WinControls.UI.PinnedRowsContainerElement.MeasureElement(IVirtualizedElement`1 element)
   at Telerik.WinControls.UI.BaseVirtualizedContainer`1.MeasureElements()
   at Telerik.WinControls.UI.BaseVirtualizedContainer`1.MeasureOverride(SizeF availableSize)
   at Telerik.WinControls.UI.ScrollableRowsContainerElement.MeasureOverride(SizeF availableSize)
   at Telerik.WinControls.RadElement.MeasureCore(SizeF availableSize)
   at Telerik.WinControls.RadElement.Measure(SizeF availableSize)
   at Telerik.WinControls.UI.RowsContainerElement.MeasureOverride(SizeF availableSize)
   at Telerik.WinControls.RadElement.MeasureCore(SizeF availableSize)
   at Telerik.WinControls.RadElement.Measure(SizeF availableSize)
   at Telerik.WinControls.UI.ScrollViewElement`1.MeasureViewElement(SizeF availableSize)
   at Telerik.WinControls.UI.ScrollViewElement`1.MeasureView(SizeF availableSize)
   at Telerik.WinControls.UI.ScrollViewElement`1.MeasureOverride(SizeF availableSize)
   at Telerik.WinControls.UI.GridTableElement.MeasureOverride(SizeF availableSize)
   at Telerik.WinControls.RadElement.MeasureCore(SizeF availableSize)
   at Telerik.WinControls.RadElement.Measure(SizeF availableSize)
   at Telerik.WinControls.Layouts.DockLayoutPanel.MeasureOverride(SizeF constraint)
   at Telerik.WinControls.RadElement.MeasureCore(SizeF availableSize)
   at Telerik.WinControls.RadElement.Measure(SizeF availableSize)
   at Telerik.WinControls.Layouts.ContextLayoutManager.UpdateLayout()
   at Telerik.WinControls.UI.GridVisibilityHelper.EnsureRowVisible(GridViewRowInfo rowInfo)
   at Telerik.WinControls.UI.GridTableElement.EnsureRowVisible(GridViewRowInfo rowInfo)
   at Telerik.WinControls.UI.GridTableElement.EnsureCellVisible(GridViewRowInfo rowInfo, GridViewColumn column)
   at Telerik.WinControls.UI.GridTableElement.UpdateViewCore(Object sender, DataViewChangedEventArgs args)
   at Telerik.WinControls.UI.GridTableElement.UpdateView(Object sender, DataViewChangedEventArgs args)
   at Telerik.WinControls.UI.GridTableElement.ProcessTemplateEvent(GridViewEvent eventData)
   at Telerik.WinControls.UI.GridTableElement.Telerik.WinControls.UI.IGridViewEventListener.ProcessEvent(GridViewEvent eventData)
   at Telerik.WinControls.UI.GridViewEventProcessEntity.ProcessCollection(GridViewEvent gridEvent, PriorityWeakReferenceList list, GridEventProcessMode processMode)
   at Telerik.WinControls.UI.GridViewEventProcessEntity.ProcessEvent(GridViewEvent gridEvent)
   at Telerik.WinControls.UI.GridViewSynchronizationService.NotifyListeners(GridViewEvent gridEvent)
   at Telerik.WinControls.UI.GridViewSynchronizationService.FlushEvents()
   at Telerik.WinControls.UI.GridViewSynchronizationService.DispatchEvent(GridViewEvent gridEvent)
   at Telerik.WinControls.UI.GridViewSynchronizationService.RaiseCurrentChanged(GridViewTemplate template, GridViewRowInfo row, GridViewColumn column, Boolean user)
   at Telerik.WinControls.UI.GridViewTemplate.CollectionView_CurrentChanged(Object sender, EventArgs e)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at Telerik.WinControls.Data.RadCollectionView`1.OnCurrentChanged(EventArgs args)
   at Telerik.WinControls.Data.RadCollectionView`1.SetCurrentPositionCore(Int32 newPosition, Boolean forceNotify, CurrentChangeReason reason)
   at Telerik.WinControls.Data.RadListSource`1.InitializeCurrentItem()
   at Telerik.WinControls.Data.RadListSource`1.Initialize()
   at Telerik.WinControls.Data.RadListSource`1.Bind(Object dataSource, String dataMember)
   at Telerik.WinControls.Data.RadListSource`1.set_DataSource(Object value)
   at Telerik.WinControls.UI.GridViewTemplate.set_DataSource(Object value)
   at BugReproduction.ControlContainingGridView.set_DataSource(Object value) in ...\BugReproduction\BugReproduction\ControlContainingGridView.cs:line 42

Attached Files:
3 comments
ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 17 Jul 2019 14:00
Hello, Corey,  

No, there is no difference. The RadGridView.DataSource property actually wraps the property of the MasterTemplate. I just replaced the element with the control in the custom implementation that you have.

Should you have further questions please let me know.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Corey
Posted on: 15 Jul 2019 14:45

Awesome! That seems to fix my issue!

I originally wanted to use a GridView, but I had initial issues and whatever example I was following implemented the Control/Element pattern.

 

I do wonder why you translated the DataSource binding to GridView.MasterTemplate.DataSource instead of just GridView.DataSource.  Is there a functional difference? Or was it because I was using the Template under GridViewElement?

Regardless, Thanks for the help!

ADMIN
Dess | Tech Support Engineer, Principal
Posted on: 15 Jul 2019 13:32
Hello, Corey,  

The provided sample project is greatly appreciated. After investigating the project I have noticed that you use a RadGridViewElement instead of a RadGridView control. The grid is not recommended to be used as an element but as a control because there is basic logic that is implemented outside the element. You can use a RadHostItem and host the RadGridView control.

I have modified the sample project to use the RadGridView control instead. Please have a look at the attached sample project. Could you please give it a try and see how it works on your end? 

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Attached Files: