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

NullReferenceException when adding new ColumnsGroup from the attached behavior

3 Answers 102 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 30 May 2013, 03:22 PM
Version: 2013.1.403
StackTrace:
System.NullReferenceException was unhandled
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=Telerik.Windows.Controls.GridView
  StackTrace:
       at Telerik.Windows.Controls.GridView.CommonHeaderPresenter.GetColumnCountToRealize(List`1 subHeaders)
       at Telerik.Windows.Controls.GridView.CommonHeaderPresenter.RealizeHeadersAtRow(Int32 row, List`1 subHeaders)
       at Telerik.Windows.Controls.GridView.CommonHeaderPresenter.RealizeHeaders()
       at Telerik.Windows.Controls.GridView.CommonHeaderPresenter.NotifyCellsPropertyChanged(String propertyName, DependencyPropertyChangedEventArgs e)
       at Telerik.Windows.Controls.GridView.GridViewHeaderRow.NotifyPropertyChanged(DependencyObject d, String propertyName, DependencyPropertyChangedEventArgs e, UpdateTarget target)
       at Telerik.Windows.Controls.GridView.GridViewDataControl.NotifyPropertyChanged(DependencyObject d, String propertyName, DependencyPropertyChangedEventArgs e, UpdateTarget target)
       at Telerik.Windows.Controls.GridViewColumnCollectionInternal.CalculateColumnWidths(Object arg)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.DispatcherOperation.InvokeImpl()
       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.Windows.Threading.DispatcherOperation.Invoke()
       at System.Windows.Threading.Dispatcher.ProcessQueue()
       at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run()
       at PlayingTelerik.App.Main() in d:\work\Sandbox\PlayingTelerik\PlayingTelerik\obj\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       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()
  InnerException: 


3 Answers, 1 is accepted

Sort by
0
Alex
Top achievements
Rank 1
answered on 30 May 2013, 03:53 PM
problem exists when adding new ColumnGroups when Window was loaded

<Window x:Class="PlayingTelerik.DynamicGrid"
        xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
        Title="DynamicGrid" Height="300" Width="300" Loaded="DynamicGrid_OnLoaded">
  <Grid>
    <telerik:RadGridView AutoGenerateColumns="False" Name="grid" >
    </telerik:RadGridView>
  </Grid>
</Window>

Code without exception
public partial class DynamicGrid : Window
    {
        public DynamicGrid()
        {
            InitializeComponent();
            GenerateColumns();
        }
 
        private void DynamicGrid_OnLoaded(object sender, RoutedEventArgs e)
        {
           /// GenerateColumns();
        }
 
        private void GenerateColumns()
        {
 
            grid.Columns.Clear();
            grid.ColumnGroups.Clear();
 
            grid.ColumnGroups.Add(new GridViewColumnGroup() { Header = "Group", Name = "grp" });
 
            grid.Columns.Add(new GridViewDataColumn()
            {
                Header = "Yep",
                ColumnGroupName = "grp"
            });
 
        }
    }
}

Code with exception

public partial class DynamicGrid : Window
    {
        public DynamicGrid()
        {
            InitializeComponent();
            // GenerateColumns();
        }
 
        private void DynamicGrid_OnLoaded(object sender, RoutedEventArgs e)
        {
            GenerateColumns();
        }
 
        private void GenerateColumns()
        {
 
            grid.Columns.Clear();
            grid.ColumnGroups.Clear();
 
            grid.ColumnGroups.Add(new GridViewColumnGroup() { Header = "Group", Name = "grp" });
 
            grid.Columns.Add(new GridViewDataColumn()
            {
                Header = "Yep",
                ColumnGroupName = "grp"
            });
 
        }
    }

0
Alex
Top achievements
Rank 1
answered on 30 May 2013, 04:29 PM
Next code also doesn't work properly. (Column groups headers are hidden)

<Window x:Class="PlayingTelerik.DynamicGrid"
        xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
        Title="DynamicGrid" Height="300" Width="300" Loaded="DynamicGrid_OnLoaded">
  <Grid>
    <telerik:RadGridView AutoGenerateColumns="False" Name="grid" >
      <telerik:RadGridView.ColumnGroups>
        <telerik:GridViewColumnGroup Header="First" Name="first" />
        <telerik:GridViewColumnGroup Header="Second" Name="second" />
      </telerik:RadGridView.ColumnGroups>
    </telerik:RadGridView>
  </Grid>
</Window>

private void DynamicGrid_OnLoaded(object sender, RoutedEventArgs e)
       {
           grid.Columns.Add(new GridViewDataColumn()
           {
               Header = "Yep",
               ColumnGroupName = "first"
           });
           grid.Columns.Add(new GridViewDataColumn()
           {
               Header = "Yep",
               ColumnGroupName = "second"
           });
       }

0
Alex
Top achievements
Rank 1
answered on 30 May 2013, 04:32 PM
Crazy but working code.

<Window x:Class="PlayingTelerik.DynamicGrid"
        xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
        Title="DynamicGrid" Height="300" Width="300" Loaded="DynamicGrid_OnLoaded">
  <Grid>
    <telerik:RadGridView AutoGenerateColumns="False" Name="grid" >
      <telerik:RadGridView.ColumnGroups>
        <telerik:GridViewColumnGroup Header="First" Name="first" />
        <telerik:GridViewColumnGroup Header="Second" Name="second" />
      </telerik:RadGridView.ColumnGroups>
      <telerik:RadGridView.Columns>
        <telerik:GridViewColumn />
      </telerik:RadGridView.Columns>
    </telerik:RadGridView>
  </Grid>
</Window>

private void DynamicGrid_OnLoaded(object sender, RoutedEventArgs e)
       {
           grid.Columns.Clear();
           grid.Columns.Add(new GridViewDataColumn()
           {
               Header = "Yep",
               ColumnGroupName = "first"
           });
           grid.Columns.Add(new GridViewDataColumn()
           {
               Header = "Yep",
               ColumnGroupName = "second"
           });
 
           // without next code headers are not updated
           grid.ReorderColumns(0, grid.Columns.Count - 1);
           grid.ReorderColumns(grid.Columns.Count - 1, 0);
       }
Tags
GridView
Asked by
Alex
Top achievements
Rank 1
Answers by
Alex
Top achievements
Rank 1
Share this question
or