第一步,创建一个文件夹: image 添加三个图标图像,分别为: add.ico,delete.ico,edit.ico
第二步:新建一个窗体为:CustomerListUI.xaml
设计入下:
代码如下:
CustomerListUI.xaml.cs的代码如下:
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 ExecuteReader执行查询.DAL;using ExecuteReader执行查询.Model;namespace ExecuteReader执行查询{ ////// CustomerListUI.xaml 的交互逻辑 /// public partial class CustomerListUI : Window { public CustomerListUI() { InitializeComponent(); } private void LoadData() { CustomerDAL dal = new CustomerDAL(); dgCustomers.ItemsSource = dal.GetAll(); } private void Window_Loaded(object sender, RoutedEventArgs e) { //CustomerDAL dal = new CustomerDAL(); //dgCustomers.ItemsSource=dal.GetAll(); LoadData(); } private void btnAdd_Click(object sender, RoutedEventArgs e) { CustomerEditUI editUI = new CustomerEditUI(); editUI.IsInsert=true; if(editUI.ShowDialog()==true) { LoadData(); } } private void btnDelete_Click(object sender, RoutedEventArgs e) { Customer customer = (Customer)dgCustomers.SelectedItem; if (customer == null) { MessageBox.Show("请选择要删除的行!"); return; } if (MessageBox.Show("确认删除此条数据吗?", "提醒", MessageBoxButton.YesNo) == MessageBoxResult.Yes) { new CustomerDAL().DeleteById(customer.Id); //再次刷新数据,因为与Window_Loaded()事件代码相同,重复使用,所以将此代码封装成为LoadData()方法 //CustomerDAL dal = new CustomerDAL(); //dgCustomers.ItemsSource = dal.GetAll(); LoadData(); } } private void btnEdit_Click(object sender, RoutedEventArgs e) { Customer customer =(Customer)dgCustomers.SelectedItem; if (customer == null) { MessageBox.Show("请选择要编辑的行"); return; } CustomerEditUI editUI = new CustomerEditUI(); editUI.IsInsert = false; editUI.EditingId = customer.Id; if (editUI.ShowDialog() == true) { LoadData(); } } }}
第三步:新建一个窗体为:CustomerEditUI.xaml,设计如下:
代码如下:
CustomerEditUI.xaml .cs的代码如下:
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 ExecuteReader执行查询.Model;using ExecuteReader执行查询.DAL;namespace ExecuteReader执行查询{ ////// CustomerEditUI.xaml 的交互逻辑 /// public partial class CustomerEditUI : Window { //判断是新增数据还是修改数据,因为新增和修改时候打开的都是编辑这一页面 public bool IsInsert { get; set; } //如果是编辑的话,需要得到编辑行的Id public long EditingId { get; set; } public CustomerEditUI() { InitializeComponent(); } private void btnSave_Click(object sender, RoutedEventArgs e) { if (IsInsert) { //用户修改过程中会自动将界面的修改同步到Customer对象中。 Customer customer = (Customer)gridEditUI.DataContext;//因为在窗体加载时候已经创建了一个对象,现在只是接收它即可。 //gridEditUI.DataContext指向的Customer对象。 //Customer customer = new Customer(); //customer.Name = txtName.Text; //customer.TelNum = txtTelNum.Text; //customer.Address = txtAddress.Text; //customer.CustLevel = Convert.ToInt32(txtCustomerLevel.Text); //customer.BirthDay = dpBirthDay.SelectedDate; //插入数据库 new CustomerDAL().Insert(customer); } else//先从数据库中查询旧的值,然后把界面上的值设置到旧对象上,Update更新。 { Customer customer = (Customer)gridEditUI.DataContext; CustomerDAL dal = new CustomerDAL(); //Customer customer=dal.GetById(EditingId); //customer.Name = txtName.Text; //customer.TelNum = txtTelNum.Text; //customer.Address = txtAddress.Text; //customer.CustLevel = Convert.ToInt32(txtCustomerLevel.Text); //customer.BirthDay = dpBirthDay.SelectedDate; //更新 dal.Update(customer); } DialogResult = true; } private void btnCancel_Click(object sender, RoutedEventArgs e) { DialogResult = false; } private void Window_Loaded(object sender, RoutedEventArgs e) { if (IsInsert) { //txtCustomerLevel.Text = "2";//默认为2级用户 Customer cust=new Customer(); cust.CustLevel = 2; gridEditUI.DataContext = cust; } else//编辑修改,将数据从数据库中拿出来,填充到页面上 { //可以把Customer直接在ListUI中传过来,这样还省得查一次数据库。 //但是一个原则:在窗口传值和容器中存储值,尽量传简单数据类型。 Customer customer = new CustomerDAL().GetById(EditingId); //txtName.Text = customer.Name; //txtTelNum.Text = customer.TelNum; //txtCustomerLevel.Text = customer.CustLevel.ToString(); //txtAddress.Text = customer.Address; //dpBirthDay.SelectedDate = customer.BirthDay; gridEditUI.DataContext = customer; } } }}
注意:其中涉及一些数据绑定的知识。