csharp WPF数据网格CRUD

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp WPF数据网格CRUD相关的知识,希望对你有一定的参考价值。

namespace WpfEnroll
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        dbConnectionDB _db = new dbConnectionDB();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource studentViewSource =
                ((System.Windows.Data.CollectionViewSource)(this.FindResource("studentViewSource")));
            // Load data by setting the CollectionViewSource.Source property:
            // studentViewSource.Source = [generic data source]
            studentViewSource.Source = _db.Query<student>("SELECT * FROM student").ToList();
        }

        private void studentDataGrid_CurrentCellChanged(object sender, EventArgs e)
        {
            /*
            try
            {
                _db.Save((student)studentDataGrid.SelectedItem);
            }
            catch (Exception)
            {
                return;
            }
            */
            
            studentDataGrid.CanUserAddRows = false;
            foreach (student x in studentDataGrid.Items)
            {
                _db.Update(x);
            }
        }

        private void SubmitButton_Copy_Click(object sender, RoutedEventArgs e)
        {
            var test = (student)studentDataGrid.SelectedItem;
            test.StudentFirstName = "KUY KAK";
            //_db.Update(test);
            studentDataGrid.Items.Refresh();
        }

        private void studentDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                foreach (student x in studentDataGrid.SelectedItems)
                {
                    _db.Delete(x);
                }
            }
        }
    }
}

以上是关于csharp WPF数据网格CRUD的主要内容,如果未能解决你的问题,请参考以下文章

使用实体模型和 LINQ 的 C# WPF SQL 多表 CRUD

雷林鹏分享:jQuery EasyUI 应用 - 创建 CRUD 数据网格(DataGrid)

如何将选定的行从一个 wpf 数据网格复制到另一个 wpf 数据网格? [关闭]

DataGrid单元格更改后,ObservableCollection不会更新

WPF数据网格粘贴

WPF 数据网格样式