关于wpf DataGrid 的增加删除行操作

本文介绍了如何在WPF中利用DataGrid进行动态增删行操作,通过绑定数组并根据数组变化更新DataGrid显示。同时,文章展示了如何从这些行数据中提取信息并直接保存到数据库。

http://bbs.youkuaiyun.com/topics/390876617 原帖问题,居然回复不来了

最后 通过duanzi_peng 的建议 绑定了一个 数组   通过对数组的增加删 来实现 DataGrid的行数变化, 然后遍历出这个数组的 类,然后直接将这个类包含的值 插入数据库     下面是我的代码,需要的朋友可以参考下

[code=csharp]
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.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Controls.Primitives;
using HRMSys.Model;
using HRMSys.DAL;
using System.Collections;
using System.Data;
using System.ComponentModel;


namespace HRMSys.UI.Scanning
{
    /// <summary>
    /// FchengjianWindow.xaml 的交互逻辑
    /// </summary>
    public partial class FchengjianWindow : Window        
    {
        public string EditingId { get; set; }  
        public ObservableCollection<Student> StuList { get; set; }   //动态数组
        public FchengjianWindow()
        {
            InitializeComponent();
            StuList = new ObservableCollection<Student>();
            this.DataContext = this;
            grd.Focus();
        }
        public class Student : INotifyPropertyChanged   
        {
            
            public int FId { get; set; }
            
            public string InNumber{ get; set; }


            #region 属性更改通知
            public event PropertyChangedEventHandler PropertyChanged;
            private void Changed(string PropertyName)
            {
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
         
### 如何在 WPF DataGrid删除选中的WPF 应用程序中,要实现从 `DataGrid` 中删除选中的的功能,可以利用 `DataGrid` 提供的选择机制以及绑定的数据源。以下是具体方法: #### 方法概述 为了删除选中的,通常需要完成以下几个部分的工作: 1. 设置 `DataGrid` 的选择模式和单位。 2. 定义按钮或其他触发器用于执删除操作。 3. 编写逻辑以移除选定的项。 --- #### XAML 配置 首先,在 XAML 文件中配置 `DataGrid` 并添加一个按钮用于触发删除操作。以下是一个简单的示例: ```xml <Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <StackPanel> <!-- 数据网格 --> <DataGrid Name="dataGrid" SelectionMode="Extended" SelectionUnit="FullRow" CanUserAddRows="False" /> <!-- 删除按钮 --> <Button Content="Delete Selected Rows" Click="DeleteSelectedRows_Click" Margin="5"/> </StackPanel> </Window> ``` 在此配置中,设置了 `SelectionMode="Extended"` 和 `SelectionUnit="FullRow"` 来允许多选择并基于整操作[^2]。 --- #### 后台代码 (C#) 接下来是在后台文件中编写删除逻辑的部分。假设 `DataGrid` 是通过绑定到一个可观察集合(如 `ObservableCollection<T>`)来填充数据,则可以直接对该集合调用 `Remove` 或 `RemoveAll` 方法。 ```csharp using System.Collections.ObjectModel; using System.Linq; using System.Windows; namespace WpfApp { public partial class MainWindow : Window { private ObservableCollection<Person> people; // 假设这是您的数据模型 public MainWindow() { InitializeComponent(); // 初始化数据集 people = new ObservableCollection<Person> { new Person { Id = 1, Name = "Alice", Age = 25 }, new Person { Id = 2, Name = "Bob", Age = 30 }, new Person { Id = 3, Name = "Charlie", Age = 35 } }; dataGrid.ItemsSource = people; // 将数据绑定到 DataGrid } private void DeleteSelectedRows_Click(object sender, RoutedEventArgs e) { var selectedItems = dataGrid.SelectedItems.Cast<Person>().ToList(); // 获取所有被选中的项目 foreach (var item in selectedItems) { people.Remove(item); // 移除每一项 } MessageBox.Show($"{selectedItems.Count} 已成功删除!"); } } public class Person { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } } ``` 上述代码实现了以下功能: - 使用了一个自定义类 `Person` 作为数据模型。 - 创建了一个 `ObservableCollection<Person>` 实例,并将其绑定到 `DataGrid` 上。 - 当点击 “Delete Selected Rows” 按钮时,会遍历当前选中的并将它们从集合中移除[^4]。 --- #### 注意事项 1. **数据上下文更新**:由于使用的是 `ObservableCollection`,当其中的内容发生变化时,UI 自动刷新而无需额外干预。 2. **错误处理**:建议增加异常捕获逻辑以防意外情况发生。 3. **性能优化**:对于大量数据的操作场景,应考虑分页显示或虚拟化技术提升用户体验。 --- ### 示例总结 此方案展示了如何通过绑定方式管理 `DataGrid` 的数据源,并借助简单界面交互实现动态删除为。这种方法不仅易于维护还具有良好的扩展性。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值