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

来源:互联网 发布:网络阅读缺点 编辑:程序博客网 时间:2024/05/22 13:21

http://bbs.csdn.net/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));
            }
            #endregion


        }
        
        private void grd_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter )
            {
                
            }


            if (e.Key == Key.F5)   
            {   
                foreach (Student tq in StuList)
                {
                    FinishJincang fc = new FinishJincang();
                    fc.RunNO = Convert.ToInt64(EditingId);//获得界面流程卡号 
                    try
                    {
                        fc.InNumber = Convert.ToDouble(tq.InNumber);
                        if (fc.InNumber == 0)
                        {
                            MessageBox.Show("第 " + tq.FId + " 行输入的的米数有误,请重新输入");
                            return;
                        }
                    }
                    catch
                    {
                        MessageBox.Show("第 " + tq.FId + " 行输入的的米数有误,请重新输入");
                        return;
                    }
                    fc.Import = 1;
                    fc.Indate = System.DateTime.Now;


                    new FinishJincangDAL().Insert(fc);
                }
                System.Windows.Forms.MessageBox.Show("保存成功");
                


            }




            if (e.Key == Key.Back)  //删除行       
            {                
                for (int i = 0; i < StuList.Count; i++)//不用for会报错 
                {
                    foreach (Student tq in StuList)  //删除选中行
                    {
                        StuList.RemoveAt(tq.FId);
                        break;
                    }


                }
                
               
            }


        }


        private void grd_InitializingNewItem(object sender, InitializingNewItemEventArgs e)  
        {
            // 序列号
            if (StuList == null || StuList.Count == 0)
            {
                ((Student)e.NewItem).FId = 1;
            }
            else
            {
                ((Student)e.NewItem).FId = StuList.Max(p => p.FId) + 1;
            }
        }


        private void grd_Loaded(object sender, RoutedEventArgs e)
        {            //加载的时候就控制焦点 在第一行
                DataGridCell cell = GetCell(0, 1);
                if (cell != null)
                {
                    cell.Focus();
                    grd.BeginEdit();
                }            
        }




        /// <summary>
        /// 根据行、列索引取的对应单元格对象
        /// </summary>
        /// <param name="row"></param>
        /// <param name="column"></param>
        /// <returns></returns>
        public DataGridCell GetCell(int row, int column)
        {
            DataGridRow rowContainer = GetRow(row);


            if (rowContainer != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);


                // try to get the cell but it may possibly be virtualized
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                if (cell == null)
                {
                    // now try to bring into view and retreive the cell
                    grd.ScrollIntoView(rowContainer, grd.Columns[column]);
                    cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                }
                return cell;
            }
            return null;
        }


        /// <summary>
        /// 根据行索引取的行对象
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public DataGridRow GetRow(int index)
        {
            DataGridRow row = (DataGridRow)grd.ItemContainerGenerator.ContainerFromIndex(index);
            if (row == null)
            {
                // may be virtualized, bring into view and try again
                grd.ScrollIntoView(grd.Items[index]);
                row = (DataGridRow)grd.ItemContainerGenerator.ContainerFromIndex(index);
            }
            return row;
        }


        /// <summary>
        /// 获取指定类型的子元素
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parent"></param>
        /// <returns></returns>
        static T GetVisualChild<T>(Visual parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }
    }
}

0 1