title: Get Wpf DataGrid data in a list
body: <p>i have a <code>datagrid</code> and i am populating it with data from a list. now i want to get back all the data in my <code>datagrid</code> to a list. how can i ?  </p>
  
  <pre><code>&lt;DataGrid Name="InstrumentGrid"
                        VerticalAlignment="Top" HorizontalAlignment="left" 
                        Margin="0,16,0,0" Grid.ColumnSpan="3" Grid.Row="19" Grid.RowSpan="2" 
                        AutoGenerateColumns="False"
                        Width="422" Grid.Column="1"&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn Header="ID" Binding="{Binding Id}" /&gt;
          &lt;DataGridTemplateColumn Header="NCS Date" &gt;
              &lt;DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;DatePicker BorderThickness="0" Height="20" /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGridTemplateColumn.CellTemplate&gt;
          &lt;/DataGridTemplateColumn&gt;
          &lt;DataGridTextColumn Header="Instrument No" Binding="{Binding NcsNo}" /&gt;
          &lt;DataGridTextColumn Header="Type" Binding="{Binding Type}" /&gt;
          &lt;DataGridTextColumn Header="Bank Name" Width="*" Binding="{Binding BankName}" /&gt;
          &lt;DataGridTemplateColumn Header="Action"&gt;
              &lt;DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;VirtualizingStackPanel Orientation="Horizontal" HorizontalAlignment="Center"&gt;
                          &lt;Image Width="10" Margin="5 0 0 0" Height="10" Stretch="Fill" Source="/InstrumentManagement;component/wrongSign.png"&gt;&lt;/Image&gt;
                      &lt;/VirtualizingStackPanel&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGridTemplateColumn.CellTemplate&gt;
          &lt;/DataGridTemplateColumn&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;  
  </code></pre>
  
  <p><strong>Loading data into DataGrid</strong>  </p>
  
  <pre><code>private List&lt;Data&gt; list = new List&lt;Data&gt;();
  public VehicleInOutWnd()
  {
      InitializeComponent();
  
  
  
      for (int i = 0; i &lt; 2; i++)
      {
          list.Add(new Data()
          {
              Id = i.ToString(),
              BankName = "Bank al habib"+i,
              NcsDate = "23/12/2018",
              InstrumentNo = "234"+i,
              Type = "Type abc"+i
          });
      }
  
      InstrumentGrid.ItemsSource = list;
  
  }  
  </code></pre>
  
  <p>Now i just want to get all this data in <code>DataGrid</code>, as it was in <code>list&lt;Data&gt;</code> in some other list. on a <code>button</code> click function.</p>