We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

GridDataControl: Bind Dynamic Column in code behind

Hi,
 
I need to bind dynamic columns into an instance of griddatacontrol. I can bind first column into the grid. However, when I tried to bind the second column the application threw me the error of "Value cannot be null".
 
Following is the code and error message.
 
CODE:
public partial class TestWindow : Window
    {
        public TestWindow()
        {
            InitializeComponent();
            Test();
        }
        private void Test()
        {
            try
            {
                InitializeComponent();
                GetDataGridTemplateColumn();
            }
            catch (Exception) { throw; }
        }
        private void GetDataGridTemplateColumn()//List<VehicleViewModel> vehicles, string columnHeader, bool canUserReorderProperty, bool isReadOnlyProperty, string converterParameter)
        {
            try
            {
                var dataGridViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("dataGridViewSource")));
                dataGridViewSource.Source = TestData();
                GridDataVisibleColumn column = new GridDataVisibleColumn();
                Binding binding1 = new Binding();
                binding1.Path = new PropertyPath("Data");
                binding1.ConverterParameter = "000";
                binding1.Converter = new ValueConverter1();
                column.Binding = binding1;
                column.DataType = DataType.String;
                column.HeaderText = "000";
                gdcInfillWorksheet.VisibleColumns.Add(column);
 
                column = new GridDataVisibleColumn();
                Binding binding2 = new Binding();
                binding2.ConverterParameter = "1111";
                binding2.Path = new PropertyPath("Data");
                binding2.Converter = new ValueConverter1();
                column.Binding = binding2;
                column.HeaderText = "1111";
                gdcInfillWorksheet.VisibleColumns.Add(column);

            }
            catch (Exception) { throw; }
        }
        private ObservableCollection<InfillWorksheetRowViewModel> TestData()
        {
            try
            {
                InfillWorksheetRowViewModel row = new InfillWorksheetRowViewModel();
                InfillWorksheetCell cell = new InfillWorksheetCell(0, 0, 0, SystemConstant.SpecificationDataType.Text, "Row 1");
                row["000"] = cell;
                cell = new InfillWorksheetCell(1, 1, 11, SystemConstant.SpecificationDataType.Text, "spec value 1");
                row["1111"] = cell;
                ObservableCollection<InfillWorksheetRowViewModel> testData = new ObservableCollection<InfillWorksheetRowViewModel>();
                testData.Add(row);
                return testData;
            }
            catch (Exception) { throw; }
        }
        private void Click_btnSave(object sender, RoutedEventArgs e)
        {
        }
    }
    public class ValueConverter1 : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            InfillWorksheetRowViewModel row = value as InfillWorksheetRowViewModel;
            string index = parameter as string;
            return row[index].Data;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception();
        }
    }
 
 
ERROR:
 
System.ArgumentNullException was unhandled
  Message=Value cannot be null.
Parameter name: source
  Source=System.Core
  ParamName=source
  StackTrace:
       at System.Linq.Enumerable.Contains[TSource](IEnumerable`1 source, TSource value, IEqualityComparer`1 comparer)
       at System.Linq.Enumerable.Contains[TSource](IEnumerable`1 source, TSource value)
       at Syncfusion.Windows.Data.EnumerableRecordsWrapper..ctor(IEnumerable source, CollectionViewAdv view)
       at Syncfusion.Windows.Data.EnumerableRecordsWrapper.CreateNew(IEnumerable source, CollectionViewAdv view)
       at Syncfusion.Windows.Controls.Grid.GridDataQueryableCollectionViewWrapper.CreateRecords()
       at Syncfusion.Windows.Data.CollectionViewAdv.EnsureInitialized()
       at Syncfusion.Windows.Data.CollectionViewAdv.get_Records()
       at Syncfusion.Windows.Controls.Grid.GridDataTableModel.RefreshSourceListCountMethod()
       at Syncfusion.Windows.Controls.Grid.GridDataTableModel.RefreshSourceListCount()
       at Syncfusion.Windows.Controls.Grid.GridDataTableModel.RefreshDisplayMethod(Boolean needsRefreshTable)
       at Syncfusion.Windows.Controls.Grid.GridDataTableModel.RefreshDisplay(Boolean needsRefreshTable)
       at Syncfusion.Windows.Controls.Grid.GridDataTableModel.EnsureInitialized()
       at Syncfusion.Windows.Controls.Grid.GridDataTableModel.get_Table()
       at Syncfusion.Windows.Controls.Grid.GridDataVisibleColumns.ValidateColumn(GridDataVisibleColumn v)
       at Syncfusion.Windows.Controls.Grid.GridDataVisibleColumns.SetTableModel(GridDataTableModel model)
       at Syncfusion.Windows.Controls.Grid.GridDataTableModel.SetSourceList(Object itemsSource, Boolean changeOriginalSource, Boolean needsWholeGridRefresh, ICollectionViewAdv collectionView)
       at Syncfusion.Windows.Controls.Grid.GridDataTableProperties.OnItemsSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
       at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.Freezable.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
       at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
       at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       at Syncfusion.Windows.Controls.Grid.GridDataTableProperties.set_ItemsSource(Object value)
       at Syncfusion.Windows.Controls.Grid.GridDataControl.HookGrid()
       at Syncfusion.Windows.Controls.Grid.GridDataControl.OnApplyTemplate()
       at System.Windows.FrameworkElement.ApplyTemplate()
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
       at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
       at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
       at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Controls.Border.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Controls.Control.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
       at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Documents.AdornerDecorator.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Controls.Border.MeasureOverride(Size constraint)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Window.MeasureOverrideHelper(Size constraint)
       at System.Windows.Window.MeasureOverride(Size availableSize)
       at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at System.Windows.UIElement.Measure(Size availableSize)
       at System.Windows.Interop.HwndSource.SetLayoutSize()
       at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
       at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
       at System.Windows.Window.SetRootVisual()
       at System.Windows.Window.SetRootVisualAndUpdateSTC()
       at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
       at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
       at System.Windows.Window.CreateSourceWindowDuringShow()
       at System.Windows.Window.SafeCreateWindowDuringShow()
       at System.Windows.Window.ShowHelper(Object booleanBox)
       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.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       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.InvokeImpl(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.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at Polk.PSDET.Client.App.Main() in D:\P&S Project\New Code\Polk.PSDET\Polk.PSDET.Client\obj\x86\Debug\App.g.cs:line 0
       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.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

3 Replies

KR Kannan R Syncfusion Team December 24, 2012 07:18 AM UTC

Hi Rubin,

Thanks for using Syncfusion products. And sorry for the delay in getting back to you.

We have analyzed your query with our sample and in this case no need to go for the converter for Visible Column binding .We have modified the sample to meet your requirement and it can be download from the following location.

Sample Location: GridDataControlSample.zip

Note: Changes done in the following files.

o    MainWindowxaml.cs

o    InfillWorksheetRowViewModel.cs

For more clarity on this query please refer the incident ID 102143.

Please let us know if you have any concerns.

Thanks,

Kannan.R



KM Kevin Miles January 9, 2014 04:00 PM UTC

I have the same kind of problem - bur the sample link does not download any more.
Can you refresh it please ?
Thanks.


SM Saravanan M Syncfusion Team January 10, 2014 11:42 AM UTC

Hi Kevin,

We apologize for the inconvenience caused.

We faced some technical issue in our server, hence the sample was not downloaded from the uploaded link. However we have prepared sample based on your requirement and you can download it from below location.

Sample Location: Grid_Data_control_sample.zip

Please let us know if you have any queries,

Regards,

Saravanan.M


Loader.
Live Chat Icon For mobile
Up arrow icon