轉(zhuǎn)帖|使用教程|編輯:龔雪|2020-12-18 13:15:31.813|閱讀 563 次
概述:DevExpress Winforms Controls 內(nèi)置140多個(gè)UI控件和庫,本文將為大家介紹主從表編輯界面的快速處理。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
DevExpress Winforms Controls 內(nèi)置140多個(gè)UI控件和庫,完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序。DevExpress WinForm v20.2全新發(fā)布,想要體驗(yàn)?點(diǎn)擊下載>>
在Winform開發(fā)中,我們往往除了常規(guī)的單表信息錄入外,有時(shí)候設(shè)計(jì)到多個(gè)主從表的數(shù)據(jù)顯示、編輯等界面,單表的信息一般就是控件和對象實(shí)體一一對應(yīng),然后調(diào)用API保存即可,主從表就需要另外特殊處理,本隨筆介紹如何快速實(shí)現(xiàn)主從表編輯界面的處理,結(jié)合GridControl控件的GridView控件對象,實(shí)現(xiàn)數(shù)據(jù)在列表中的實(shí)時(shí)編輯,非常方便。
主從表一般涉及兩個(gè)以上的表,一個(gè)是主表,其他的是從表的,在實(shí)際情況下,一般包含兩個(gè)表較多,我們這里以兩個(gè)表的主從表關(guān)系進(jìn)行分析處理。
例如我們建立兩個(gè)報(bào)銷申請單表關(guān)系如下所示。
對于報(bào)銷的主從表信息,我們可以在列表中進(jìn)行展示,如下界面所示,分為兩部分:一部分是主表信息,一部分是從表信息,單擊主表信息后,顯示對應(yīng)從表的列表信息。
那么我們新增一條主表記錄的時(shí)候,那么可以彈出一個(gè)新的界面進(jìn)行數(shù)據(jù)的維護(hù)處理,方便我們錄入主從表的信息,界面如下所示。
上面界面包括了主表信息,以及從表的信息(在GridView中實(shí)時(shí)錄入)兩部分,這樣填寫后統(tǒng)一進(jìn)行提交處理。
這里主要介紹一下主從表的編輯界面處理,也就是上面這個(gè)界面的實(shí)現(xiàn)處理。
其中初始化GridView的代碼如下所示。
/// <summary> /// 初始化明細(xì)表的GridView數(shù)據(jù)顯示 /// </summary> private void InitDetailGrid() { //初始清空列 this.gridView1.Columns.Clear(); //設(shè)置部分列隱藏 this.gridView1.CreateColumn("ID", "編號").Visible = false; this.gridView1.CreateColumn("Header_ID", "主表編號").Visible = false; this.gridView1.CreateColumn("Apply_ID", "申請單編號").Visible = false; //添加下拉列表列,并綁定數(shù)據(jù)源 this.gridView1.CreateColumn("FeeType", "費(fèi)用類型", 100).CreateComboBox().BindDictItems("費(fèi)用類型"); //創(chuàng)建日期列并指定格式 var OccurTime = this.gridView1.CreateColumn("OccurTime", "發(fā)生時(shí)間", 120).CreateDateEdit(); OccurTime.EditMask = "yyyy-MM-dd HH:mm"; OccurTime.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm"; //創(chuàng)建數(shù)值列 this.gridView1.CreateColumn("FeeAmount", "費(fèi)用金額").CreateSpinEdit(); //創(chuàng)建備注列 this.gridView1.CreateColumn("FeeDescription", "費(fèi)用說明", 200).CreateMemoEdit(); //初始化GridView,可以新增列 this.gridView1.InitGridView(GridType.NewItem, false, EditorShowMode.MouseDownFocused, ""); //轉(zhuǎn)義列內(nèi)容顯示 this.gridView1.CustomColumnDisplayText += new CustomColumnDisplayTextEventHandler(gridView1_CustomColumnDisplayText); //處理單元格的樣式 this.gridView1.RowCellStyle += new RowCellStyleEventHandler(gridView1_RowCellStyle); //不允許頭部排序 this.gridView1.OptionsCustomization.AllowSort = false; //繪制序號 this.gridView1.CustomDrawRowIndicator += (s, e) => { if (e.Info.IsRowIndicator && e.RowHandle >= 0) { e.Info.DisplayText = (e.RowHandle + 1).ToString(); } }; //對輸入單元格進(jìn)行非空校驗(yàn) this.gridView1.ValidateRow += delegate(object sender, ValidateRowEventArgs e) { var result = gridControl1.ValidateRowNull(e, new string[] { "FeeType" }); }; //新增行的內(nèi)容初始化 this.gridView1.InitNewRow += (s, e) => { gridView1.SetRowCellValue(e.RowHandle, "ID", Guid.NewGuid().ToString()); gridView1.SetRowCellValue(e.RowHandle, "Header_ID", tempInfo.ID); gridView1.SetRowCellValue(e.RowHandle, "Apply_ID", tempInfo.Apply_ID); gridView1.SetRowCellValue(e.RowHandle, "OccurTime", DateTime.Now); }; } void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e) { GridView gridView = this.gridView1; if (e.Column.FieldName == "FeeAmount") { e.Appearance.BackColor = Color.Green; e.Appearance.BackColor2 = Color.LightCyan; } } void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) { string columnName = e.Column.FieldName; if (e.Column.ColumnType == typeof(DateTime)) { if (e.Value != null) { if (e.Value == DBNull.Value || Convert.ToDateTime(e.Value) <= Convert.ToDateTime("1900-1-1")) { e.DisplayText = ""; } else { e.DisplayText = Convert.ToDateTime(e.Value).ToString("yyyy-MM-dd HH:mm");//yyyy-MM-dd } } } }
上面代碼都有詳細(xì)的備注,主要就是我們根據(jù)數(shù)據(jù)庫表的關(guān)系,創(chuàng)建對應(yīng)顯示的字段即可,其中有需要隱藏的那么就不要顯示(方便獲取對應(yīng)的值)
//設(shè)置部分列隱藏 this.gridView1.CreateColumn("ID", "編號").Visible = false; this.gridView1.CreateColumn("Header_ID", "主表編號").Visible = false; this.gridView1.CreateColumn("Apply_ID", "申請單編號").Visible = false;
如果需要綁定下拉列表類似的字段,那么創(chuàng)建對應(yīng)的數(shù)據(jù)類型,然后調(diào)用綁定函數(shù)綁定即可,如下面代碼
//添加下拉列表列,并綁定數(shù)據(jù)源 this.gridView1.CreateColumn("FeeType", "費(fèi)用類型", 100).CreateComboBox().BindDictItems("費(fèi)用類型");
如果是一些特殊的輸入需要設(shè)置格式顯示或者掩碼,那么如下所示
//創(chuàng)建日期列并指定格式 var OccurTime = this.gridView1.CreateColumn("OccurTime", "發(fā)生時(shí)間", 120).CreateDateEdit(); OccurTime.EditMask = "yyyy-MM-dd HH:mm"; OccurTime.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm";
另外有一個(gè)值得注意的就是我們新增一行從表記錄的時(shí)候,需要記錄一些主表的屬性,這樣的話,我們就是在行初始化的時(shí)候,賦值給從表的隱藏列即可。
//新增行的內(nèi)容初始化 this.gridView1.InitNewRow += (s, e) => { gridView1.SetRowCellValue(e.RowHandle, "ID", Guid.NewGuid().ToString()); gridView1.SetRowCellValue(e.RowHandle, "Header_ID", tempInfo.ID); gridView1.SetRowCellValue(e.RowHandle, "Apply_ID", tempInfo.Apply_ID); gridView1.SetRowCellValue(e.RowHandle, "OccurTime", DateTime.Now); };
在界面中如果我們需要顯示主表的信息,那么就根據(jù)條件獲取對應(yīng)的主表記錄對象,然后顯示給界面控件即可。
/// <summary> /// 顯示常規(guī)的對象內(nèi)容 /// </summary> /// <param name="info"></param> private void DisplayInfo(ReimbursementInfo info) { tempInfo = info;//重新給臨時(shí)對象賦值,使之指向存在的記錄對象 txtCategory.Text = info.Category; txtReason.Text = info.Reason; txtTotalAmount.Value = info.TotalAmount; txtNote.Text = info.Note; }
而保存的時(shí)候,我們把界面內(nèi)容重新賦值給對應(yīng)的主表對象。
/// <summary> /// 編輯或者保存狀態(tài)下取值函數(shù) /// </summary> /// <param name="info"></param> private void SetInfo(ReimbursementInfo info) { info.Category = txtCategory.Text; info.Reason = txtReason.Text; info.TotalAmount = txtTotalAmount.Value; info.Note = txtNote.Text; info.ApplyDate = DateTime.Now; info.ApplyDept = base.LoginUserInfo.DeptId; info.CurrentLoginUserId = base.LoginUserInfo.ID; }
而我們需要獲取GridView明細(xì)輸入的時(shí)候,就通過一個(gè)函數(shù)遍歷獲取GridView的行記錄,轉(zhuǎn)換為相應(yīng)的對象即可,如下所示。
/// <summary> /// 獲取明細(xì)列表 /// </summary> /// <returns></returns> private List<ReimbursementDetailInfo> GetDetailList() { var list = new List<ReimbursementDetailInfo>(); for (int i = 0; i < this.gridView1.RowCount; i++) { var detailInfo = gridView1.GetRow(i) as ReimbursementDetailInfo; if (detailInfo != null) { list.Add(detailInfo); } } return list; }
這樣處理完這些信息后,我們就可以在主表保存的時(shí)候,同時(shí)保存明細(xì)表信息即可。
/// <summary> /// 新增狀態(tài)下的數(shù)據(jù)保存 /// </summary> /// <returns></returns> public override bool SaveAddNew() { ReimbursementInfo info = tempInfo;//必須使用存在的局部變量,因?yàn)椴糠中畔⒖赡鼙桓郊褂?SetInfo(info); info.Creator = base.LoginUserInfo.ID; info.CreateTime = DateTime.Now; try { #region 新增數(shù)據(jù) bool succeed = BLLFactory<Reimbursement>.Instance.Insert(info); if (succeed) { //可添加其他關(guān)聯(lián)操作 var list = GetDetailList(); foreach(var detailInfo in list) { BLLFactory<ReimbursementDetail>.Instance.InsertUpdate(detailInfo, detailInfo.ID); } return true; } #endregion } catch (Exception ex) { LogTextHelper.Error(ex); MessageDxUtil.ShowError(ex.Message); } return false; }
其中代碼
BLLFactory<ReimbursementDetail>.Instance.InsertUpdate(detailInfo, detailInfo.ID);
可以對新增記錄保存,也可以對存在的記錄進(jìn)行更新。
通過上面的介紹,我們可以看到不同的主從表其實(shí)邏輯還是很通用的,我們可以把它們的邏輯抽取出來,通過代碼生成工具進(jìn)行快速生成即可。
本文轉(zhuǎn)載自
DevExpress技術(shù)交流群2:775869749 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: