Reputation: 23
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
<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>
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); } } } }
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; } } }
Upvotes: 0
Views: 1658
Reputation: 5161
Is this what you want?
public partial class MainWindow : Window { ObservableCollection dataGrid2Items = new ObservableCollection();
public MainWindow()
{
InitializeComponent();
dataGrid1.ItemsSource = idata;
dataGrid2.ItemsSource = dataGrid2Items;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
foreach (latlongobj item in dataGrid1.SelectedItems)
{
if( !dataGrid2Items.Contains( item ) )
dataGrid2Items.Add(item);
}
}
private ObservableCollection<latlongobj> idata = new ObservableCollection<latlongobj>
{
new latlongobj{ name = "n1", Lat = 1, Lon = 2 },
new latlongobj{ name = "n2", Lat = 2, Lon = 3 },
new latlongobj{ name = "n3", Lat = 4, Lon = 5 },
};
Upvotes: 0