如何将一个项目从一个数据网格添加到另一个数据网格,然后在 WPF 中编辑它

发布于 2024-10-01 23:12:17 字数 2398 浏览 2 评论 0原文

当我尝试使用 datagrid.items.add() 将项目从一个数据网格添加到另一个数据网格时遇到问题。基本上我有两个数据网格以主从关系起作用。第一个 DataGrid1 用于显示自动生成的列和行。第二个数据网格 DataGrid2 将在单击特定按钮时显示 DataGrid1.SelectedItems。每次单击该按钮时,我希望将 DataGrid1 中的选定项目保留在 DataGrid2 中,并且每次单击该按钮时,都会将更多项目添加到 DataGrid2 中。除了能够在 DataGrid2 上编辑单元格之外,我已经能够完成大部分要求。当我双击 DataGrid2 中的单元格时,出现异常,提示“此视图不允许 EditItem”。我读过很多关于将数据添加到 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>

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

计㈡愣 2024-10-08 23:12:17

这是你想要的吗?

公共部分类 MainWindow : 窗口
{
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 },
        };

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 },
        };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文