How to add an item from one datagrid to another then edit it.

Nov 12, 2010 at 11:24 PM
Edited Nov 12, 2010 at 11:28 PM

Hi, 

 I have an issue when I try to use datagrid.items.add() to add items from one datagrid to another. Basically I have two data grids acting in a master slave relationship. The First DataGrid1 is used to display automatically generated columns and rows. The second datagrid, DataGrid2 will display the DataGrid1.SelectedItems when a specific button is clicked. Each time the button is clicked I'd like to have the selected items from DataGrid1 stay in DataGrid2 and each time the button is clicked more items get added to DataGrid2. I have been able to complete most of my requirements with the exception of the ability to edit cells on DataGrid2. When I double click a cell in DataGrid2 I get an exception that says "EditItem' is not allowed for this view". I have read a lot of posts about adding data to a ObservableCollection, ListCollectionView and so on but either I can not implement them in the correct manner or there not working for my situation. My code is as follows and by the way thx in advance 

 

MainWindow.xaml
<Window x:Class="WpfApplication1.MainWindow"        
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"      
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"       
 Title="MainWindow" Height="350" Width="525">   
 <Grid>        
<DataGrid AutoGenerateColumns="False" Height="77" HorizontalAlignment="Left" Margin="27,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="464" />
        <Button Content="AddRow" Height="23" HorizontalAlignment="Left" Margin="27,107,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> 
       <DataGrid AutoGenerateColumns="False" Height="140" HorizontalAlignment="Left" Margin="27,159,0,0" Name="dataGrid2" VerticalAlignment="Top" Width="464" />   
 </Grid>
</Window>

Small Class (latlonobj.cs):  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
namespace WpfApplication1{    class latlonobj    
{        public string name { get; set; }        public double Lat { get; set; }       
 public double Lon { get; set; }    }}


C Sharp (MainWindow.xaml.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{    ///     /// Interaction logic for MainWindow.xaml    ///     
public partial class MainWindow : Window 
   {        public MainWindow()        {            InitializeComponent();            dataGrid1.ItemsSource = idata;                    }
        private void button1_Click(object sender, RoutedEventArgs e)        
{            foreach (latlongobj item in dataGrid1.SelectedItems)            {                dataGrid2.Items.Add(item);            }
        }    }}</button>