【问题标题】:How to add an item from one datagrid to another then edit it in WPF如何将一个数据网格中的项目添加到另一个数据网格,然后在 WPF 中对其进行编辑
【发布时间】:2010-11-13 03:55:49
【问题描述】:

当我尝试使用 datagrid.items.add() 将项目从一个数据网格添加到另一个数据网格时遇到问题。基本上我有两个数据网格在主从关系中起作用。 First DataGrid1 用于显示自动生成的列和行。第二个数据网格 DataGrid2 将在单击特定按钮时显示 DataGrid1.SelectedItems。每次单击按钮时,我都希望从 DataGrid1 中选择的项目留在 DataGrid2 中,并且每次单击按钮时,都会将更多项目添加到 DataGrid2 中。除了能够在 DataGrid2 上编辑单元格之外,我已经能够完成我的大部分要求。当我双击 DataGrid2 中的一个单元格时,我得到一个异常,上面写着“EditItem' is not allowed for this view”。我已经阅读了很多关于将数据添加到 ObservableCollection、ListCollectionView 等的帖子,但要么我无法以正确的方式实现它们,要么不适用于我的情况。我的代码如下,顺便提一下

<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>

使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Text; 使用 System.Windows; 使用 System.Windows.Controls; 使用 System.Windows.Data; 使用 System.Windows.Documents; 使用 System.Windows.Input; 使用 System.Windows.Media; 使用 System.Windows.Media.Imaging; 使用 System.Windows.Navigation; 使用 System.Windows.Shapes; 命名空间 WpfApplication1 { /// /// MainWindow.xaml 的交互逻辑 /// 公共部分类 MainWindow : 窗口 { 公共主窗口() { 初始化组件(); dataGrid1.ItemsSource = idata; } 私人无效按钮1_Click(对象发送者,RoutedEventArgs e) { foreach(dataGrid1.SelectedItems 中的 latlongobj 项) { dataGrid2.Items.Add(item); } } } }

使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Text; 命名空间 WpfApplication1 { 类 latlonobj { 公共字符串名称 { 获取;放; } 公共双纬度{得到;放; } 公共双隆{得到;放; } } }

【问题讨论】:

    标签: c# wpf datagrid wpftoolkit wpfdatagrid


    【解决方案1】:

    这是你想要的吗?

    公共部分类 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 },
            };
    

    【讨论】:

    • @David,如果这就是答案,你为什么不投票或接受答案!?
    猜你喜欢
    • 2011-09-22
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多