This is a migrated thread and some comments may be shown as answers.

GridView throws Null Reference error when GridViewComboBox column is bound to a list of objects and the list changes

1 Answer 295 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Randy
Top achievements
Rank 1
Randy asked on 17 Jul 2013, 01:58 PM
When GridView has a GridViewComboBoxColumn that is bound to a list of objects and that list changes a null reference error is thrown.  This error did not get throw in the previous release (2013 Q1), it just started happening in (2013 Q2, specifically 2013.2.612.40).

Here is a code example using a list of Authors and a list of books.  Basically the book object has a reference to the Author object.  When the author list changes the book grid will throw a null reference error.

Here is the stack trace of the error:

   at Telerik.WinControls.UI.GridViewComboBoxColumn.AddItem(Int32 i)
   at Telerik.WinControls.UI.GridViewComboBoxColumn.currencyManager_ListChanged(Object sender, ListChangedEventArgs e)
   at System.ComponentModel.ListChangedEventHandler.Invoke(Object sender, ListChangedEventArgs e)
   at System.Windows.Forms.CurrencyManager.OnListChanged(ListChangedEventArgs e)
   at System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender, ListChangedEventArgs e)
   at System.ComponentModel.BindingList`1.OnListChanged(ListChangedEventArgs e)
   at System.ComponentModel.BindingList`1.Child_PropertyChanged(Object sender, PropertyChangedEventArgs e)
   at GridTest.Author.NotifyPropertyChanged(String info) in c:\Users\SESA124669\Documents\Visual Studio 2012\Projects\GridTest\GridTest\Form1.cs:line 69
   at GridTest.Author.set_Name(String value) in c:\Users\SESA124669\Documents\Visual Studio 2012\Projects\GridTest\GridTest\Form1.cs:line 97
   at GridTest.Form1.button1_Click(Object sender, EventArgs e) in c:\Users\SESA124669\Documents\Visual Studio 2012\Projects\GridTest\GridTest\Form1.cs:line 52
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at GridTest.Program.Main() in c:\Users\SESA124669\Documents\Visual Studio 2012\Projects\GridTest\GridTest\Program.cs:line 19
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace GridTest
{
    public partial class Form1 : Form
    {
        private BindingList<Author> AuthorData = new BindingList<Author>();
        private BindingList<Book> BookData = new BindingList<Book>();
 
        public Form1()
        {
            InitializeComponent();
 
            radGridViewAuthors.DataSource = AuthorData;
 
            radGridViewBooks.MasterTemplate.Columns.Clear();
 
            GridViewComboBoxColumn colAuth = new GridViewComboBoxColumn("Author");
            colAuth.HeaderText = "Author";
            colAuth.Name = "Author";
            colAuth.DataSource = AuthorData;
            colAuth.ValueMember = "Id";
            colAuth.DisplayMember = "Name";
            colAuth.DisplayMemberSort = true;
            radGridViewBooks.MasterTemplate.Columns.Add(colAuth);
 
            GridViewTextBoxColumn colName = new GridViewTextBoxColumn("Name");
            colName.HeaderText = "Name";
            colName.Name = "Name";
            colName.ReadOnly = true;
            radGridViewBooks.MasterTemplate.Columns.Add(colName);
 
            radGridViewBooks.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
            radGridViewBooks.DataSource = BookData;
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Author m = new Author();
            m.Id = AuthorData.Count + 1;
            AuthorData.Add(m);
            m.Name = textBoxAuthorName.Text;
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            Book m = new Book(textBoxBookName.Text);
            BookData.Add(m);
        }
    }
 
    public class Author : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
 
        private int id;
        public int Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
                NotifyPropertyChanged("Id");
            }
        }
 
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
                NotifyPropertyChanged("Name");
            }
        }
 
        public Author() { }
        public Author(int id, string name)
        {
            Id = id;
            Name = name;
        }
    }
 
    public class Book : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
 
        private string name;
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
                NotifyPropertyChanged("Name");
            }
        }
 
        private Author author;
        public Author Author
        {
            get
            {
                return author;
            }
            set
            {
                author = value;
                NotifyPropertyChanged("Author");
            }
        }
 
        public Book() { }
        public Book(string name)
        {
            Name = name;
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
George
Telerik team
answered on 22 Jul 2013, 01:53 PM
Hello Randy,

Thank you for contacting us.

I have reviewed your case carefully. The exception is caused by the fact that the combobox can not initialize itself properly when the binding list is empty. I have added a feature request for that in our Public Issue Tracking System you can subscribe to track it for changes and vote for it on this address http://www.telerik.com/support/pits.aspx#/public/winforms/15413. For the time being a possible workaround is to add a dummy object to the binding list before binding it to the grid and the column and remove it after that. If the scenario with binding list is hard to maintain I would recommend you to use a DataTable. I am also uploading my test project just in case you need it. Additionally I have updated your Telerik Points as a token of gratitude for the request.

Hope this helps, if you have any other questions or comments, please let me know.
 
Regards,
George
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Randy
Top achievements
Rank 1
Answers by
George
Telerik team
Share this question
or