Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2589
NullReferenceException with ItemsSource
posted

Hello,

 

I put an empty XamWebGrid on page like this (copied this from your demo page):

 

        <igGrid:XamWebGrid x:Name="dataGrid" AutoGenerateColumns="false">

            <igGrid:XamWebGrid.Columns>

                <igGrid:TextColumn Key="ID">

                    <igGrid:TextColumn.HeaderTemplate>

                        <DataTemplate>

                            <TextBlock Text="Customer ID" />

                        </DataTemplate>

                    </igGrid:TextColumn.HeaderTemplate>

                </igGrid:TextColumn>

                <igGrid:TextColumn Key="Company">

                    <igGrid:TextColumn.HeaderTemplate>

                        <DataTemplate>

                            <TextBlock Text="Company" />

                        </DataTemplate>

                    </igGrid:TextColumn.HeaderTemplate>

                </igGrid:TextColumn>

                <igGrid:TextColumn Key="Name">

                    <igGrid:TextColumn.HeaderTemplate>

                        <DataTemplate>

                            <TextBlock Text="Name" />

                        </DataTemplate>

                    </igGrid:TextColumn.HeaderTemplate>

                </igGrid:TextColumn>

 

            </igGrid:XamWebGrid.Columns>

        </igGrid:XamWebGrid>

 

 

and do some actions in code:

        ObservableCollection<Person> _persons = new ObservableCollection<Person>();

 

        public MainPage()

        {

            _persons.Add(new Person(1, "Adam Smith", "Coca-cola"));

            _persons.Add(new Person(2, "John Michaels", "Pepsi-cola"));

            _persons.Add(new Person(3, "Mary James", "Boo-cola"));

            InitializeComponent();  

            this.Loaded += new RoutedEventHandler(PersonsLoaded);

        }

 

        private void PersonsLoaded(object sender, RoutedEventArgs e)

        {

            dataGrid.ItemsSource = _persons;            

        }

When I run application I get a NullReferenceException in PersonsLoaded method. When I inspect ItemsSource property, I see that it has been set to a proper value. But anyway I get that error, no matter if I set XamWebGrid.Columns manually or set AutoGenerateColumns to TRUE. Can you please tell me what's wrong?

 

 

Thank you.

  • 21382
    Verified Answer
    posted

    I tried out the situation you described with the following mock up and did not see your reported results.  I tried it out against both the release version of the DLL and a current dev build of the control.  Could you post the stack trace from the debugger so we can see where the NRE is raised?  Which version of the DLL are you using?  Are you turning on any other feature besides the ones listed?

     

                ObservableCollection<Person> _persons = new ObservableCollection<Person>();

                public MainPage()

                {

                      InitializeComponent();

                      _persons.Add(new Person(1, "Adam Smith", "Coca-cola"));

                      _persons.Add(new Person(2, "John Michaels", "Pepsi-cola"));

                      _persons.Add(new Person(3, "Mary James", "Boo-cola"));

                      this.Loaded += new RoutedEventHandler(PersonsLoaded);

                }

     

                private void PersonsLoaded(object sender, RoutedEventArgs e)

                {

                      dataGrid.ItemsSource = _persons;

                }

     

          }

     

          public class Person

          {

                public int ID { get; set; }

                public string Company { get; set; }

                public string Name { get; set; }

     

                public Person(int id, string name, string comp)

                {

                      this.ID = id;

                      this.Name = name;

                      this.Company = comp;

                }

          }

     

    <UserControl x:Class="SilverlightApplication9.MainPage"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

                      xmlns:igGrid="clr-namespace:Infragistics.Silverlight.Controls;assembly=Infragistics.Silverlight.XamWebGrid.v9.1"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

          <Grid x:Name="LayoutRoot">

                <igGrid:XamWebGrid x:Name="dataGrid" AutoGenerateColumns="false">

                      <igGrid:XamWebGrid.Columns>

                            <igGrid:TextColumn Key="ID">

                                  <igGrid:TextColumn.HeaderTemplate>

                                        <DataTemplate>

                                              <TextBlock Text="Customer ID" />

                                        </DataTemplate>

                                  </igGrid:TextColumn.HeaderTemplate>

                            </igGrid:TextColumn>

                            <igGrid:TextColumn Key="Company">

                                  <igGrid:TextColumn.HeaderTemplate>

                                        <DataTemplate>

                                              <TextBlock Text="Company" />

                                        </DataTemplate>

                                  </igGrid:TextColumn.HeaderTemplate>

                            </igGrid:TextColumn>

                            <igGrid:TextColumn Key="Name">

                                  <igGrid:TextColumn.HeaderTemplate>

                                        <DataTemplate>

                                              <TextBlock Text="Name" />

                                        </DataTemplate>

                                  </igGrid:TextColumn.HeaderTemplate>

                            </igGrid:TextColumn>

                      </igGrid:XamWebGrid.Columns>

                </igGrid:XamWebGrid>

          </Grid>

    </UserControl>