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

SelectedItem Object Reference not set

1 Answer 99 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Francois
Top achievements
Rank 1
Francois asked on 06 Aug 2009, 10:43 AM
Working with: Version 2009.1.526.35

Trying to set SelectedItem.
Code:

<telerik:RadGridView AutoGenerateColumns="False"
                             SelectedItem="{Binding Path=Entity, Mode=TwoWay}" IsReadOnly="False"
            ItemsSource="{Binding Path=Entities}"

In my constructor of my model I create a instance of Entity (Selected) and a List of Entities(ItemSource).

When I run, I get a Object Reference not Set... exception.
The same code works fine with a ListBox.

1 Answer, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 10 Aug 2009, 10:30 AM
Hello Francois,

I have tried to reproduce this behavior with the latest version but I could not. Here is part of my sample project:

<Window x:Class="TicketID_233145_SelectedItemBinding.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
    Title="Window1" Height="300" Width="300"
    <Grid> 
        <Grid.RowDefinitions> 
            <RowDefinition Height="Auto"/> 
            <RowDefinition Height="Auto"/> 
        </Grid.RowDefinitions> 
        <Button Content="Display Selected Club"  
                Click="OnDisplaySelected"  
                Margin="10"  
                HorizontalAlignment="Left"/> 
        <telerik:RadGridView  
            Name="clubsGrid"  
            Grid.Row="1" 
            ItemsSource="{Binding Path=Clubs}" 
            SelectedItem="{Binding Path=SelectedClub, Mode=TwoWay}"
        </telerik:RadGridView> 
    </Grid> 
</Window> 
 

using System.Collections.Generic; 
using System.Linq; 
using System.Windows; 
 
namespace TicketID_233145_SelectedItemBinding 
    /// <summary> 
    /// Interaction logic for Window1.xaml 
    /// </summary> 
    public partial class Window1 : Window 
    { 
        public Window1() 
        { 
            this.InitializeComponent(); 
 
            this.SelectedClub = this.Clubs.Where(c => c.Name == "Chelsea").FirstOrDefault(); 
 
            this.clubsGrid.DataContext = this
        } 
 
        public IEnumerable<Club> Clubs 
        { 
            get { return Club.ClubsDataSource; } 
        } 
 
        public Club SelectedClub { getset; } 
 
        private void OnDisplaySelected(object sender, RoutedEventArgs e) 
        { 
            MessageBox.Show("The currently selected club is " + this.SelectedClub.Name); 
        } 
    } 

I have attached the whole project. Can you run it on your end and tell me the results. Also, can you check whether this will be resolved if you upgrade to the latest internal build.

All the best,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
Francois
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
Share this question
or