轉(zhuǎn)帖|使用教程|編輯:我只采一朵|2016-03-31 14:09:43.000|閱讀 1869 次
概述:本教程帶你深入理解 DevExpress 日程管理控件 XtraScheduler數(shù)據(jù)存儲(chǔ)。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷售中 >>
相關(guān)鏈接:
在上篇隨筆《DevExpress XtraScheduler日程管理控件應(yīng)用實(shí)例》 中介紹了 DevExpress 的XtraScheduler日程控件的各種使用知識(shí)點(diǎn),對(duì)于我們來(lái)說(shuō),日程控件不陌生,如OutLook里面就有日歷的模塊,但是這個(gè)日程控件真的是很復(fù)雜的一個(gè)控件,需要全面掌握可能需要花費(fèi)很多的時(shí)間去了解,由于是技術(shù)研究,我總是希望把它常用的功能剖析的更加徹底一 些,前面隨筆也介紹了它的存儲(chǔ)功能,把它基于實(shí)體類的方式存儲(chǔ)在數(shù)據(jù)庫(kù)里面,不過(guò)介紹的還不夠,本文繼續(xù)上面的內(nèi)容,進(jìn)行數(shù)據(jù)存儲(chǔ)方面的介紹。
| 立即下載DevExpress安裝包,免費(fèi)體驗(yàn)30天!
在查閱了大量資料,以及一兩天的潛入研究,總算把它的數(shù)據(jù)存儲(chǔ)和相關(guān)熟悉有一個(gè)比較清晰的了解。
在上篇隨筆里面,我總體性介紹了這個(gè)控件的數(shù)據(jù)綁定,以及數(shù)據(jù)是如何保存到數(shù)據(jù)庫(kù)里面的,綁定到DevExpress的XtraScheduler日程控件的步驟是需要先設(shè)置好映射關(guān)系(Mappings),然后綁定數(shù)據(jù)源即可。
操作代碼如下所示。
/// <summary> /// 設(shè)置日程控件的字段映射 /// </summary> /// <param name="control">日程控件</param> private void SetMappings(SchedulerControl control) { AppointmentMappingInfo appoint = control.Storage.Appointments.Mappings; appoint.AllDay = "AllDay"; appoint.Description = "Description"; appoint.End = "EndDate"; appoint.Label = "AppLabel"; appoint.Location = "Location"; appoint.RecurrenceInfo = "RecurrenceInfo"; appoint.ReminderInfo = "ReminderInfo"; appoint.ResourceId = "ResourceId"; appoint.Start = "StartDate"; appoint.Status = "Status"; appoint.Subject = "Subject"; appoint.Type = "EventType"; ResourceMappingInfo res = control.Storage.Resources.Mappings; res.Caption = "ResourceName"; res.Color = "Color"; res.Id = "ResourceId"; res.Image = "Image"; }
然后接著就是綁定Appointment和Resource到對(duì)應(yīng)的數(shù)據(jù)源里面接口。
//從數(shù)據(jù)庫(kù)加載日程信息 List<AppResourceInfo> resouceList = BLLFactory<AppResource>.Instance.GetAll(); this.schedulerStorage1.Resources.DataSource = resouceList; List<UserAppointmentInfo> eventList = BLLFactory<UserAppointment>.Instance.GetAll(); this.schedulerStorage1.Appointments.DataSource = eventList;
但是,上面這樣的存儲(chǔ)在是實(shí)際上是比較少的,也就是我們往往可能會(huì)在界面上進(jìn)行新增或者復(fù)制記錄,修改記錄,或者刪除記錄等操作,因此需要進(jìn)一步利用日程控件的完善接口來(lái)處理這些操作。
我們?cè)赩S的對(duì)應(yīng)控件屬性里面可以看到一些關(guān)于存儲(chǔ)的重要事件,也就是日程的增刪改處理事件,如下所示。
上面這幾個(gè)事件也就是對(duì)應(yīng)在日程控件里面右鍵菜單對(duì)應(yīng)的增刪改操作。
另外日程控件還可以支持拖動(dòng)修改、拖動(dòng)復(fù)制、刪除鍵刪除操作的,這些也是會(huì)繼續(xù)調(diào)用上面那些增刪改的操作事件的,所以我們就對(duì)他們進(jìn)行完善,我們重點(diǎn)是處理ing類型的事件,如Inserting的事件,在寫(xiě)入日程控件集合之前的處理。
//寫(xiě)回?cái)?shù)據(jù)庫(kù)操作的事件 control.Storage.AppointmentInserting += Storage_AppointmentInserting; control.Storage.AppointmentChanging += Storage_AppointmentChanging; control.Storage.AppointmentDeleting += Storage_AppointmentDeleting;
對(duì)于修改數(shù)據(jù)前的處理,我們是讓它在順利寫(xiě)入數(shù)據(jù)庫(kù)后,在決定是否更新日程對(duì)象的存儲(chǔ)集合還是丟棄修改記錄,如下所示。
void Storage_AppointmentChanging(object sender, PersistentObjectCancelEventArgs e) { Appointment apt = e.Object as Appointment; UserAppointmentInfo info = ConvertToAppoint(apt); bool success = BLLFactory<UserAppointment>.Instance.Update(info, apt.Id); e.Cancel = !success; }
注意上面的e.Cancel =true或者false代表是否放棄,上面的代碼邏輯就是如果我們順利寫(xiě)入數(shù)據(jù)庫(kù),那么就可以成功更新到日程控件的存儲(chǔ)集合里面,而且就可以在界面看到最新的結(jié)果。
有了上面的理解,我們就可以進(jìn)一步完善在插入前、刪除前的代碼處理了。
對(duì)于刪除前的操作,我們可以用的代碼如下所示。
void Storage_AppointmentDeleting(object sender, PersistentObjectCancelEventArgs e) { Appointment apt = e.Object as Appointment; if (apt != null && apt.Id != null) { if (MessageDxUtil.ShowYesNoAndWarning("您確認(rèn)要?jiǎng)h除該記錄嗎?") == DialogResult.Yes) { bool success = BLLFactory<UserAppointment>.Instance.Delete(apt.Id); e.Cancel = !success; } } }
我們使用代碼MessageDxUtil.ShowYesNoAndWarning來(lái)判斷是否繼續(xù),如下界面所示。
對(duì)于插入的記錄,我們需要更加注意,需要寫(xiě)入數(shù)據(jù)庫(kù)后,進(jìn)行本地的存儲(chǔ)記錄的更新,這樣才能合理顯示,否則容易發(fā)生復(fù)制、創(chuàng)建的記錄位置總是不對(duì),偏移到其他地方去的。
void Storage_AppointmentInserting(object sender, PersistentObjectCancelEventArgs e) { Appointment apt = e.Object as Appointment; UserAppointmentInfo info = ConvertToAppoint(apt); bool success = BLLFactory<UserAppointment>.Instance.Insert(info); e.Cancel = !success; if (success) { LoadData(); } }
LoadData就是我們從數(shù)據(jù)庫(kù)加載日程信息,并綁定到日程控件的存儲(chǔ)對(duì)象里面,其中需要注意的就是需要使用RefreshData方法,讓日程控件的存儲(chǔ)對(duì)象刷新一下,這樣才能夠順利顯示我們添加的記錄。
//從數(shù)據(jù)庫(kù)加載日程信息 List<AppResourceInfo> resouceList = BLLFactory<AppResource>.Instance.GetAll(); this.schedulerStorage1.Resources.DataSource = resouceList; List<UserAppointmentInfo> eventList = BLLFactory<UserAppointment>.Instance.GetAll(); this.schedulerStorage1.Appointments.DataSource = eventList;
this.schedulerControl1.RefreshData();//必須,每次修改需要刷新數(shù)據(jù)源,否則界面需要重新刷新
在日程控件里面,支持多人資源的處理,默認(rèn)是資源只能選擇其一,需要多人的話,那么就需要設(shè)置下面的屬性來(lái)顯示聲明使用多人資源,如下所示。
schedulerControl1.Storage.Appointments.ResourceSharing = true;
使用多人的資源,可以對(duì)資源進(jìn)行復(fù)選,它的映射記錄就是ResourceIds的了,所以設(shè)置映射屬性的時(shí)候,我們需要判斷這個(gè)ResourceSharing 屬性。
if(control.ResourceSharing) { appoint.ResourceId = "ResourceIds"; } else { appoint.ResourceId = "ResourceId"; }
其中ResourceId的內(nèi)容格式如下所示
<ResourceIds> <ResourceId Type="System.String" Value="1" /><ResourceId Type="System.String" Value="2" /> </ResourceIds>
和ResourceId不同這里的值就是一個(gè)XML內(nèi)容,這個(gè)和提醒等內(nèi)容的存儲(chǔ)格式一樣,都是基于XML的內(nèi)容。日程控件涉及到的幾種XML的信息獲取如下所示。
//多人資源的信息 if(apt.ResourceIds != null) { AppointmentResourceIdCollectionContextElement item = new AppointmentResourceIdCollectionContextElement(apt.ResourceIds); info.ResourceIds = item.ValueToString(); //第二種 //AppointmentResourceIdCollectionXmlPersistenceHelper helper = new AppointmentResourceIdCollectionXmlPersistenceHelper(apt.ResourceIds); //info.ResourceIds = helper.ToXml(); } //日程重復(fù)信息 if (apt.RecurrenceInfo != null) { info.RecurrenceInfo = apt.RecurrenceInfo.ToXml(); } //提醒信息 if (apt.Reminder != null) { info.ReminderInfo = ReminderCollectionXmlPersistenceHelper.CreateSaveInstance(apt).ToXml(); }
以上就是我們經(jīng)常用到的日程控件的處理內(nèi)容了,希望對(duì)大家有所幫助。
by
更多DevExpress資源請(qǐng)關(guān)注
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn