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

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

 

 

作者:夏春涛 xchunta@163.com

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

 

4.6 界面层HomeShop.WinForm

 

 

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="HomeShop" connectionString="Data Source=(local);Initial Catalog=HomeShop;Integrated Security=SSPI;" />
  </connectionStrings>
</configuration>

 

 

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

 

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

 

 

数据库文件:/Files/SummerRain/NetDbDevRoad/HomeShopDB.rar

完整源代码:/Files/SummerRain/NetDbDevRoad/4使用ADONET实现三层架构Table.rar

 

 

上一篇:【Spring注解驱动开发】自定义TypeFilter指定@ComponentScan注解的过滤规则


下一篇:分页