[Silverlight]主-明細檔製作(資料刪除、更新,利用ObservableCollection顯示資料異動)
(新增專案)
(建立資料來源EDM(Ado.Net Data Modal)、WcfDataService)
(※完成設定後,記得對專案進行建置(F6),不然在[加入資料來源]時,會發生錯誤)
(加入資料來源)
(程式碼)
1: using Silverlight_Master_Detail.ServiceReferenceSchool;
2: using System.Collections.ObjectModel; //ObservableCollection
3: using System.Data.Services.Client; //DataServiceQuery
4:
5: namespace Silverlight_Master_Detail
6: {
7: public partial class MainPage : UserControl
8: {
9: private SchoolEntities schoolEntity;
10: //當資料進行刪除時,dataGrid資料列並不會移除,
11: //所以要用ObservableCollection<T>來存放資料,並與dataGrid.ItemsSource資料繫結
12: //再用ObservableCollection<T>.Remove(entity object)、RemoveAt(int Index)、Clear()清除資料、Add(entity object)新增,
13: //進行異動資料,就可以看到dataGrid資料列的異動狀態。
14: private ObservableCollection<Order_Master> orderMasterCollection; //主檔
15: private ObservableCollection<Order_Detail> orderDetailCollection = new ObservableCollection<Order_Detail>(); //明細檔
16: public MainPage()
17: {
18: InitializeComponent();
19:
20: schoolEntity = new SchoolEntities(new Uri("WcfDataServiceSchool.svc",UriKind.Relative));
21:
22: grdMaster.SelectionChanged += new SelectionChangedEventHandler(grdMaster_SelectionChanged);
23: grdMaster.KeyDown += new KeyEventHandler(grdMaster_KeyDown);
24: grdDetail.KeyDown += new KeyEventHandler(grdDetail_KeyDown);
25: btnDelMaster.Click += new RoutedEventHandler(btnDelMaster_Click);
26: btnDelDetail.Click += new RoutedEventHandler(btnDelDetail_Click);
27: btnSave.Click += new RoutedEventHandler(btnSave_Click);
28: //畫面初始,進行訂單主檔查詢,並用方法Expend()將明細做一併查詢
29: AsyncCallback AsyncMaster = (asyncR) =>
30: {
31: DataServiceQuery result = asyncR.AsyncState as DataServiceQuery;
32: orderMasterCollection = new ObservableCollection<Order_Master>(result.EndExecute(asyncR).Cast<Order_Master>());
33: grdMaster.ItemsSource = orderMasterCollection;
34: if (orderMasterCollection.Count>0)
35: {
36: grdMaster.SelectedIndex = 0;
37: }
38: };
39: DataServiceQuery masterQuery = schoolEntity.Order_Master.Expand("Order_Detail").Select(q => q) as DataServiceQuery;
40: masterQuery.BeginExecute(AsyncMaster, masterQuery);
41: }
42: //每當選取grdMaster資料列時,更新orderDetailCollection,無資料時清除orderDetailCollection
43: void grdMaster_SelectionChanged(object sender, SelectionChangedEventArgs e)
44: {
45: if (grdMaster.SelectedItem != null)
46: {
47: orderDetailCollection = (grdMaster.SelectedItem as Order_Master).Order_Detail;
48: grdDetail.ItemsSource = orderDetailCollection;
49: }
50: else
51: {
52: orderDetailCollection.Clear();
53: }
54: }
55: //當dataGrid 有keyDown事件,就當做有資料異動
56: void grdMaster_KeyDown(object sender, KeyEventArgs e)
57: {
58: schoolEntity.UpdateObject(grdMaster.SelectedItem);
59: }
60: //當dataGrid 有keyDown事件,就當做有資料異動
61: void grdDetail_KeyDown(object sender, KeyEventArgs e)
62: {
63: schoolEntity.UpdateObject(grdDetail.SelectedItem);
64: }
65: //將異動資料儲存
66: void btnSave_Click(object sender, RoutedEventArgs e)
67: {
68: AsyncCallback AsyncSave = (asyncR) =>
69: {
70: schoolEntity = asyncR.AsyncState as SchoolEntities;
71: schoolEntity.EndSaveChanges(asyncR);
72: };
73: schoolEntity.BeginSaveChanges(AsyncSave, schoolEntity);
74: }
75: //刪除整筆資料
76: void btnDelMaster_Click(object sender, RoutedEventArgs e)
77: {
78: if (grdMaster.SelectedItem != null)
79: {
80: Order_Master delItem = grdMaster.SelectedItem as Order_Master;
81: if (delItem != null)
82: {
83: schoolEntity.DeleteObject(delItem);
84: orderMasterCollection.Remove(grdMaster.SelectedItem as Order_Master);
85: }
86: }
87: }
88: //刪除明細資料
89: void btnDelDetail_Click(object sender, RoutedEventArgs e)
90: {
91: if (grdDetail.SelectedItem != null)
92: {
93: schoolEntity.DeleteObject(grdDetail.SelectedItem);
94: orderDetailCollection.Remove(grdDetail.SelectedItem as Order_Detail);
95: }
96: }
97: }
98: }
(測試)