title: Getting selected Item in DataGrid in WPF
body: <p>I have a datagrid and textboxes. When I select a row in DataGrid I want the textboxes to be filled with related data of that datagrid column. Here is the picture.,</p>
  
  <p><img src="https://i.stack.imgur.com/R58yl.jpg" alt="enter image description here"></p>
  
  <p>I have written this code in MainWindow.xaml.cs : </p>
  
  <pre><code>private void personelEntityDataGrid_Loaded(object sender, RoutedEventArgs e)
  {
      PersonelEntity pers = (PersonelEntity)personelEntityDataGrid.SelectedItem;
  
      NameBox.Text = pers.Name; // I get exception here
      AgeBox.Text = pers.Age.ToString();
      PhoneNumberBox.Text = pers.PhoneNumber;
      AddresBox.Text = pers.Address;
  
  
  }
  </code></pre>
  
  <p>When I run the code I get the null reference exception in the line that I wrote. Here is the exception:</p>
  
  <blockquote>
    <p>{"Object reference not set to an instance of an object."}</p>
  </blockquote>
  
  <p>I guess the object pers is null. Can you tell me how to make this work?</p>
  
  <p>Here is my xaml file : </p>
  
  <pre><code>&lt;Window
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Personel" mc:Ignorable="d" x:Class="Personel.MainWindow"
          Title="MainWindow" Height="500" Width="725" Loaded="Window_Loaded"&gt;
      &lt;Window.Resources&gt;
          &lt;CollectionViewSource x:Key="personelEntityViewSource" d:DesignSource="{d:DesignInstance {x:Type local:PersonelEntity}, CreateList=True}"/&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid DataContext="{StaticResource personelEntityViewSource}" &gt;
          &lt;Label Content="Personnel
                 " HorizontalAlignment="Left" Margin="55,47,0,0" VerticalAlignment="Top"/&gt;
          &lt;Button Content="Delete" HorizontalAlignment="Left" Margin="344,47,0,0" VerticalAlignment="Top" Width="75" Click="Delete_Button_Click"/&gt;
          &lt;Button Content="Add" HorizontalAlignment="Left" Margin="477,47,0,0" VerticalAlignment="Top" Width="75" Click="Add_Button_Click"/&gt;
          &lt;Button Content="Update" HorizontalAlignment="Left" Margin="624,47,0,0" VerticalAlignment="Top" Width="75" Click="Update_Button_Click"/&gt;
          &lt;Label Content="Name" HorizontalAlignment="Left" Margin="55,91,0,0" VerticalAlignment="Top"/&gt;
          &lt;Label Content="Age" HorizontalAlignment="Left" Margin="55,132,0,0" VerticalAlignment="Top"/&gt;
          &lt;Label Content="Phone Number" HorizontalAlignment="Left" Margin="55,178,0,0" VerticalAlignment="Top"/&gt;
          &lt;Label Content="Address" HorizontalAlignment="Left" Margin="55,218,0,0" VerticalAlignment="Top"/&gt;
          &lt;TextBox HorizontalAlignment="Left" Height="23" Margin="309,95,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="367" Name="NameBox"/&gt;
          &lt;TextBox HorizontalAlignment="Left" Height="23" Margin="309,136,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="57" Name="AgeBox"/&gt;
          &lt;TextBox HorizontalAlignment="Left" Height="23" Margin="309,178,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Name="PhoneNumberBox"/&gt;
          &lt;TextBox HorizontalAlignment="Left" Height="23" Margin="309,222,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="367" Name="AddresBox"/&gt;
          &lt;DataGrid x:Name="personelEntityDataGrid" AutoGenerateColumns="False" SelectionChanged="personelEntityDataGrid_SelectionChanged"  EnableRowVirtualization="True" ItemsSource="{Binding Path=.}" Margin="10,291,0,-22" RowDetailsVisibilityMode="VisibleWhenSelected" Loaded="personelEntityDataGrid_Loaded"&gt;
              &lt;DataGrid.Columns&gt;
                  &lt;DataGridTextColumn x:Name="addressColumn" Binding="{Binding Address}" Header="Address" Width="SizeToHeader"/&gt;
                  &lt;DataGridTextColumn x:Name="ageColumn" Binding="{Binding Age}" Header="Age" Width="SizeToHeader"/&gt;
                  &lt;DataGridTextColumn x:Name="idColumn" Binding="{Binding Id}" Header="Id" Width="SizeToHeader"/&gt;
                  &lt;DataGridTextColumn x:Name="nameColumn" Binding="{Binding Name}" Header="Name" Width="SizeToHeader"/&gt;
                  &lt;DataGridTextColumn x:Name="phoneNumberColumn" Binding="{Binding PhoneNumber}" Header="Phone Number" Width="SizeToHeader"/&gt;
              &lt;/DataGrid.Columns&gt;
          &lt;/DataGrid&gt;
  
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>