天天看點

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

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;

        }

    }

}

繼續閱讀