.NET数据库编程求索之路5.使用ADO.NET实现(三层架构篇使用List传递数据)(3)

5.使用ADO.NET实现(三层架构篇-使用List传递数据)(2)

作者:夏春涛 xchunta@163.com

转载请注明来源:http://www.cnblogs.com/SummerRain/archive/2012/07/26/2610957.html

5.4 界面层HomeShop.WinForm

FormMain.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 //新添命名空间
 10 using HomeShop.Model;
 11 using HomeShop.BLL;
 12 
 13 namespace HomeShop.WinForm
 14 {
 15     //功能:订单管理系统-主界面窗体;
 16     //作者:夏春涛;
 17     //日期:2011-12-02;
 18     public partial class FormMain : Form
 19     {
 20         //Order业务逻辑对象
 21         private OrderBiz orderBiz = new OrderBiz();
 22 
 23         //【窗体构造函数】
 24         public FormMain()
 25         {
 26             InitializeComponent();            
 27         }
 28 
 29         //【窗体加载事件】
 30         private void FormMain_Load(object sender, EventArgs e)
 31         {   
 32             gridView.AutoGenerateColumns = false;//禁止DataGridView控件自动生成列         
 33             BindData_Orders();//绑定订单列表
 34         }
 35 
 36         //绑定订单列表
 37         private void BindData_Orders()
 38         {
 39             txtCustomerName.Clear();
 40             List<Order> list = orderBiz.GetList();
 41             gridView.DataSource = list;
 42             labRecordCount.Text = "共 " + list.Count.ToString() + " 条记录";
 43         }
 44 
 45         //【查询】订单
 46         private void btnQuery_Click(object sender, EventArgs e)
 47         {
 48             if ("" == txtCustomerName.Text.Trim())
 49             {
 50                 MessageBox.Show(this"请输入要查询的顾客姓名关键词!""提示"
 51                                 MessageBoxButtons.OK, MessageBoxIcon.Information);
 52                 txtCustomerName.Focus();
 53                 return;
 54             }
 55             List<Order> list = orderBiz.GetList(txtCustomerName.Text.Trim());
 56             gridView.DataSource = list;
 57             labRecordCount.Text = "共 " + list.Count.ToString() + " 条记录";
 58         }
 59 
 60         //【全部显示】订单
 61         private void btnShowAll_Click(object sender, EventArgs e)
 62         {
 63             BindData_Orders();
 64         }
 65 
 66         //【新增】订单
 67         private void menuAdd_Click(object sender, EventArgs e)
 68         {
 69             FormEdit formEdit = new FormEdit();
 70             DialogResult dlgResult = formEdit.ShowDialog(this);//显示为模式对话框
 71             if (DialogResult.OK == dlgResult)
 72             {
 73                 BindData_Orders();//重新绑定订单列表
 74             }
 75             formEdit.Dispose();//释放窗体资源
 76         }
 77 
 78         //【修改】订单
 79         private void menuUpdate_Click(object sender, EventArgs e)
 80         {
 81             if (gridView.RowCount == 0return;
 82 
 83             int selectedRowIndex = gridView.SelectedRows[0].Index;
 84             int selectedOrderID = (int)gridView.SelectedRows[0].Cells["Col_OrderID"].Value;
 85             FormEdit formEdit = new FormEdit(selectedOrderID);
 86             DialogResult dlgResult = formEdit.ShowDialog(this);//显示为模式对话框
 87             if (DialogResult.OK == dlgResult)
 88             {
 89                 BindData_Orders();//重新绑定订单列表
 90                 gridView.Rows[selectedRowIndex].Selected = true;//依然选中当前行
 91             }
 92             formEdit.Dispose();//释放窗体资源
 93         }
 94 
 95         //【删除】订单
 96         private void menuDelete_Click(object sender, EventArgs e)
 97         {
 98             if (gridView.RowCount == 0return;
 99 
100             int selectedRowIndex = gridView.SelectedRows[0].Index;
101             int selectedOrderID = (int)gridView.SelectedRows[0].Cells["Col_OrderID"].Value;
102             DialogResult dlgResult = MessageBox.Show(this"确认要删除选中的订单吗?""提示"
103                                                      MessageBoxButtons.YesNo, MessageBoxIcon.Question);
104             if (DialogResult.Yes == dlgResult)
105             {
106                 orderBiz.Delete(selectedOrderID);//删除订单
107                 BindData_Orders();//重新绑定订单列表
108                 //选中下一条记录
109                 if (selectedRowIndex > gridView.Rows.Count - 1)
110                     selectedRowIndex = gridView.Rows.Count - 1;
111                 if (selectedRowIndex >= 0)
112                 {
113                     gridView.Rows[selectedRowIndex].Selected = true;
114                 }
115             }         
116         }
117 
118         
119     }
120 }

FormEdit.cs

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 //新添命名空间
 10 using System.Configuration;
 11 using HomeShop.Model;
 12 using HomeShop.BLL;
 13 
 14 namespace HomeShop.WinForm
 15 {
 16     //功能:订单管理系统-新增/修改订单窗体;
 17     //作者:夏春涛;
 18     //日期:2011-12-02;
 19     public partial class FormEdit : Form
 20     {
 21         //Order业务逻辑对象
 22         private OrderBiz orderBiz = new OrderBiz();
 23 
 24         //当前订单对象
 25         private Order order = new Order();
 26 
 27         //临时ID,起始值为0,自动递减(避免与正常的正整数ID重复)
 28         private int tempID = 0;
 29 
 30         //【窗体构造函数】
 31         public FormEdit()
 32         {
 33             InitializeComponent();
 34             //----
 35             this.Text = "新增订单";//窗体标题            
 36         }
 37 
 38         //【窗体构造函数】-重载,入参:要修改的订单的ID
 39         public FormEdit(int orderID)
 40         {
 41             InitializeComponent();
 42             //----
 43             this.Text = "修改订单";//窗体标题 
 44             this.order = orderBiz.GetSingle(orderID);             
 45         }
 46 
 47         //【窗体加载事件】-初始化控件的内容
 48         private void FormEdit_Load(object sender, EventArgs e)
 49         {
 50             gridView.AutoGenerateColumns = false;//禁止DataGridView控件自动生成列                     
 51             InitOrderState();//初始化订单状态下拉框            
 52             InitOrderInfo();//初始化订单信息
 53         }
 54 
 55         //初始化订单状态下拉框
 56         private void InitOrderState()
 57         { 
 58             OrderStateBiz orderStateBiz = new OrderStateBiz();
 59             OrderState.DataSource = orderStateBiz.GetList();
 60             OrderState.ValueMember = "Code";
 61             OrderState.DisplayMember = "Name";
 62         }
 63 
 64         //初始化订单基本信息
 65         private void InitOrderInfo()
 66         {
 67             //将当前订单基本信息显示在编辑区
 68             OrderID.Text = order.OrderID.ToString();
 69             CustomerName.Text = order.CustomerName;
 70             CustomerPhoneNo.Text = order.CustomerPhoneNo;
 71             CustomerAddress.Text = order.CustomerAddress;
 72             OrderTime.Value = order.OrderTime;
 73             OrderState.SelectedValue = order.OrderStateCode;
 74             OrderTotal.Text = order.OrderTotal.ToString();//获取订单总金额
 75             //初始化当前订单关联的商品列表
 76             gridView.DataSource = order.OrderItems;
 77         }
 78    
 79         //【新增】商品
 80         private void btnProductAdd_Click(object sender, EventArgs e)
 81         {
 82             if (!ValidateInput_Product()) return;
 83 
 84             OrderItem orderItem = new OrderItem();
 85             orderItem.OrderID = this.order.OrderID;
 86             orderItem.OrderItemID = tempID--;
 87             orderItem.Product = Product.Text.Trim();
 88             orderItem.UnitPrice = Convert.ToDecimal(UnitPrice.Text.Replace(" """));
 89             orderItem.Quantity = Convert.ToInt32(Quantity.Text.Replace(" """));
 90             this.order.OrderItems.Add(orderItem);
 91             
 92             gridView.DataSource = new List<OrderItem>();
 93             gridView.DataSource = this.order.OrderItems;
 94             OrderTotal.Text = this.order.OrderTotal.ToString();
 95             gridView.Rows[gridView.Rows.Count - 1].Selected = true;
 96         }
 97 
 98         //验证用户输入-新增/修改商品信息时
 99         private bool ValidateInput_Product()
100         {
101             //验证商品名称
102             if ("" == Product.Text.Trim())
103             {
104                 MessageBox.Show(this"请输入商品名称!""提示",
105                                 MessageBoxButtons.OK, MessageBoxIcon.Information);
106                 Product.Focus();
107                 return false;
108             }
109             //验证商品单价            
110             try
111             {
112                 string regexString = @"^[0-9]*[0-9]+[\.]*[0-9]*$";//正则表达式-非负数
113                 RegexStringValidator validator = new RegexStringValidator(regexString);
114                 validator.Validate(UnitPrice.Text.Replace(" """));
115             }
116             catch
117             {
118                 MessageBox.Show(this"请输入正确的商品单价(非负数)!""提示",
119                                 MessageBoxButtons.OK, MessageBoxIcon.Information);
120                 UnitPrice.Focus();
121                 return false;
122             }
123             //验证商品数量            
124             try
125             {
126                 string regexString = @"^[0-9]*[1-9][0-9]*$";//正则表达式-正整数
127                 RegexStringValidator validator = new RegexStringValidator(regexString);
128                 validator.Validate(Quantity.Text.Replace(" """));
129             }
130             catch
131             {
132                 MessageBox.Show(this"请输入正确的商品数量(正整数)!""提示",
133                                 MessageBoxButtons.OK, MessageBoxIcon.Information);
134                 Quantity.Focus();
135                 return false;
136             }
137 
138             return true;
139         }
140 
141         //【修改】选中的商品
142         private void btnProductUpdate_Click(object sender, EventArgs e)
143         {
144             if (gridView.RowCount == 0return;
145             if (!ValidateInput_Product()) return;
146 
147             int selectedRowIndex = gridView.SelectedRows[0].Index;
148 
149             int orderItemID = (int)gridView.Rows[selectedRowIndex].Cells["Col_OrderItemID"].Value;
150             OrderItem orderItem = this.order.OrderItems.Find(
151                                       delegate(OrderItem item){ return item.OrderItemID == orderItemID; });            
152             orderItem.Product = Product.Text.Trim();
153             orderItem.UnitPrice = Convert.ToDecimal(UnitPrice.Text.Replace(" """));
154             orderItem.Quantity = Convert.ToInt32(Quantity.Text.Replace(" """));
155 
156             gridView.DataSource = new List<OrderItem>();
157             gridView.DataSource = this.order.OrderItems;
158             OrderTotal.Text = this.order.OrderTotal.ToString();
159             gridView.Rows[selectedRowIndex].Selected = true;
160         }
161 
162         //【删除】选中的商品
163         private void btnProductDelete_Click(object sender, EventArgs e)
164         {
165             if (gridView.RowCount == 0return;            
166             
167             DialogResult dlgResult = MessageBox.Show(this"确认要删除选中的商品吗?""提示"
168                                                 MessageBoxButtons.YesNo, MessageBoxIcon.Question);
169             if (DialogResult.Yes == dlgResult)
170             {
171                 int selectedRowIndex = gridView.SelectedRows[0].Index; 
172                 
173                 int orderItemID = (int)gridView.SelectedRows[0].Cells["Col_OrderItemID"].Value;
174                 OrderItem orderItem = this.order.OrderItems.Find(
175                                           delegate(OrderItem item){ return item.OrderItemID == orderItemID; });
176                 this.order.OrderItems.Remove(orderItem);
177 
178                 gridView.DataSource = new List<OrderItem>();
179                 gridView.DataSource = this.order.OrderItems;
180                 OrderTotal.Text = this.order.OrderTotal.ToString();
181                 //选中下一条记录
182                 if (selectedRowIndex > gridView.Rows.Count - 1)
183                     selectedRowIndex = gridView.Rows.Count - 1;
184                 if (selectedRowIndex >= 0)
185                 {
186                     gridView.Rows[selectedRowIndex].Selected = true;
187                     ShowSelectedRowInfo();//将选中的商品信息显示在编辑区
188                 }
189             }
190         }
191 
192         //【选择更改事件】-将选中的商品信息显示在编辑区
193         private void gridView_SelectionChanged(object sender, EventArgs e)
194         {
195             ShowSelectedRowInfo();
196         }
197 
198         //将选中的商品信息显示在编辑区
199         private void ShowSelectedRowInfo()
200         {
201             if (gridView.SelectedRows.Count == 0return;
202             Product.Text = gridView.SelectedRows[0].Cells["Col_Product"].Value.ToString();
203             UnitPrice.Text = gridView.SelectedRows[0].Cells["Col_UnitPrice"].Value.ToString();
204             Quantity.Text = gridView.SelectedRows[0].Cells["Col_Quantity"].Value.ToString();
205         }        
206 
207         //【确定】-保存新增/修改的订单
208         private void btnOK_Click(object sender, EventArgs e)
209         {
210             if (!ValidateInput_Order()) return;//验证用户输入
211 
212             order.OrderStateCode = OrderState.SelectedValue.ToString();
213             order.OrderTime = OrderTime.Value;
214             order.CustomerAddress = CustomerAddress.Text.Trim();
215             order.CustomerName = CustomerName.Text.Trim();
216             order.CustomerPhoneNo = CustomerPhoneNo.Text.Trim();
217 
218             if (0 == this.order.OrderID)//新增订单
219             {
220                 orderBiz.Add(this.order);
221                 MessageBox.Show(this"新增订单成功!""提示",
222                     MessageBoxButtons.OK, MessageBoxIcon.Information);
223             }
224             else //修改订单
225             {
226                 orderBiz.Update(this.order);
227                 MessageBox.Show(this"修改订单成功!""提示",
228                     MessageBoxButtons.OK, MessageBoxIcon.Information);
229             }
230 
231             this.DialogResult = DialogResult.OK;//设置对话框结果
232             this.Close();//关闭窗体
233         }
234 
235         //验证用户输入-保存新增/修改的订单时
236         private bool ValidateInput_Order()
237         {
238             //验证顾客姓名
239             if ("" == CustomerName.Text.Trim())
240             {
241                 MessageBox.Show(this"请输入顾客姓名!""提示",
242                                 MessageBoxButtons.OK, MessageBoxIcon.Information);
243                 CustomerName.Focus();
244                 return false;
245             }
246             //验证联系电话
247             if ("" == CustomerPhoneNo.Text.Trim())
248             {
249                 MessageBox.Show(this"请输入联系电话!""提示",
250                                 MessageBoxButtons.OK, MessageBoxIcon.Information);
251                 CustomerPhoneNo.Focus();
252                 return false;
253             }
254             //订购商品信息
255             if (0 == gridView.Rows.Count)
256             {
257                 MessageBox.Show(this"请输入订购商品信息!""提示",
258                                 MessageBoxButtons.OK, MessageBoxIcon.Information);
259                 Product.Focus();
260                 return false;
261             }
262 
263             return true;
264         }
265         
266         //【取消】-关闭窗体
267         private void btnCancel_Click(object sender, EventArgs e)
268         {
269             this.Close();//关闭窗体
270         }
271     }
272 }
原文地址:https://www.cnblogs.com/SummerRain/p/2610957.html