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

Filter throws error Object reference not set to an instance of an object.

1 Answer 613 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Vinayak
Top achievements
Rank 1
Vinayak asked on 03 Jun 2014, 06:32 AM
Hi All ,
I am getting the error "Object reference not set to an instance of an object." when trying to filter on gridview.
my functional requirement is to referesh grid data on every 5 seconds. I get this error when i keep open filter popup  above 5 seconds and trying to filter the data. However its working if i try to filter within 5 seconds.

 Please help to suggest .

The followings are the stacktrace details for your reference:

System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
  Source="Telerik.WinControls.GridView"
  StackTrace:
       at Telerik.WinControls.UI.GridHeaderCellElement.filterPopup_FilterConfirmed(Object sender, EventArgs e)
       at Telerik.WinControls.UI.BaseFilterPopup.OnFilterConfirmed()
       at Telerik.WinControls.UI.RadListFilterPopup.OnButtonOkClick(EventArgs e)
       at Telerik.WinControls.RadItem.OnClick(EventArgs e)
       at Telerik.WinControls.UI.RadButtonItem.OnClick(EventArgs e)
       at Telerik.WinControls.UI.RadButtonElement.OnClick(EventArgs e)
       at Telerik.WinControls.RadItem.DoClick(EventArgs e)
       at Telerik.WinControls.RadItem.RaiseBubbleEvent(RadElement sender, RoutedEventArgs args)
       at Telerik.WinControls.ComponentInputBehavior.OnMouseUp(MouseEventArgs e)
       at Telerik.WinControls.RadControl.OnMouseUp(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at Telerik.WinControls.RadControl.WndProc(Message& m)
       at Telerik.WinControls.UI.RadPopupControlBase.WndProc(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(Int32 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 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at WindowsApplication2.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 05 Jun 2014, 01:51 PM
Hello Vinayak,

Thank you for writing.

Your question has already been answered in the support thread you have opened. However, I am posting the answer here again, so the community can benefit from it.

When you change the RadGridView.DataSource on Timer.Tick while you are performing Excel-like filtering, at some point the grid does not have data source (it is reset as its DataSource is set to null/Nothing) and the grid is filtered by non-existing values. This caused the problem you encountered. In order to avoid this undesired behavior, you can suspend the data source reset operation using a flag on the FilterPopupInitialized event and resume it when the RadListFilterPopup.PopupClosed event is fired.

I suppose that you are trying to reflect any changes in RadGridView, coming from the DataSource. It is not necessary to reset the data source. You can change the underlying data source and perform filtering simultaneously without setting the DataSource property to null. In order to reflect any data changes and keep the same data source, please refer to our Reflecting Custom Object Changes in RGV help article. Here is a sample code snippet, demonstrating how to refresh the bound data and continue with filtering without any difficulties. The provided video (drag and drop over the browser to play) illustrates better the implemented functionality:
BindingList<Item> items = new BindingList<Item>();
 
public Form1()
{
    InitializeComponent();
    for (int i = 0; i < 5; i++)
    {
        items.Add(new Item(Guid.NewGuid().ToString(),i + ".Data"));
    }
 
    this.radGridView1.DataSource = items;
    this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
    this.radGridView1.EnableFiltering = true;
    this.radGridView1.MasterTemplate.ShowHeaderCellButtons = true;
    this.radGridView1.MasterTemplate.ShowFilteringRow = false;
 
    this.timer1.Start();
}
 
Random rand = new Random();
 
private void timer1_Tick(object sender, EventArgs e)
{
    items[rand.Next(0, items.Count)].UniqueIdentifier = Guid.NewGuid().ToString();
    items[rand.Next(0, items.Count)].UniqueIdentifier = Guid.NewGuid().ToString();
}
 
public class Item: System.ComponentModel.INotifyPropertyChanged
{
    public string UniqueIdentifier
    {
        get
        {
            return this._uniqueIdentifier;
        }
        set
        {
            this._uniqueIdentifier = value;
            OnPropertyChanged("UniqueIdentifier");
        }
    }
 
    public string Name
    {
        get
        {
            return this._name;
        }
        set
        {
            this._name = value;
            OnPropertyChanged("Name");
        }
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
 
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
 
    private string _uniqueIdentifier;
    private string _name;
 
    public Item(string uniqueIdentifier, string name)
    {
        this.UniqueIdentifier = uniqueIdentifier;
        this.Name = name;
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Vinayak
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or