Winform界面中主从表编辑界面的快速处理
在Winform開發中,我們往往除了常規的單表信息錄入外,有時候設計到多個主從表的數據顯示、編輯等界面,單表的信息一般就是控件和對象實體一一對應,然后調用API保存即可,主從表就需要另外特殊處理,本隨筆介紹如何快速實現主從表編輯界面的處理,結合GridControl控件的GridView控件對象,實現數據在列表中的實時編輯,非常方便。
1、主從表的界面設計及展示
主從表一般涉及兩個以上的表,一個是主表,其他的是從表的,在實際情況下,一般包含兩個表較多,我們這里以兩個表的主從表關系進行分析處理。
例如我們建立兩個報銷申請單表關系如下所示。
對于報銷的主從表信息,我們可以在列表中進行展示,如下界面所示,分為兩部分:一部分是主表信息,一部分是從表信息,單擊主表信息后,顯示對應從表的列表信息。
那么我們新增一條主表記錄的時候,那么可以彈出一個新的界面進行數據的維護處理,方便我們錄入主從表的信息,界面如下所示。
上面界面包括了主表信息,以及從表的信息(在GridView中實時錄入)兩部分,這樣填寫后統一進行提交處理。
?2、主從表編輯界面的處理
這里主要介紹一下主從表的編輯界面處理,也就是上面這個界面的實現處理。
其中初始化GridView的代碼如下所示。
/// <summary> /// 初始化明細表的GridView數據顯示/// </summary>private void InitDetailGrid(){//初始清空列this.gridView1.Columns.Clear();//設置部分列隱藏this.gridView1.CreateColumn("ID", "編號").Visible = false;this.gridView1.CreateColumn("Header_ID", "主表編號").Visible = false;this.gridView1.CreateColumn("Apply_ID", "申請單編號").Visible = false;//添加下拉列表列,并綁定數據源this.gridView1.CreateColumn("FeeType", "費用類型", 100).CreateComboBox().BindDictItems("費用類型");//創建日期列并指定格式var OccurTime = this.gridView1.CreateColumn("OccurTime", "發生時間", 120).CreateDateEdit();OccurTime.EditMask = "yyyy-MM-dd HH:mm";OccurTime.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm";//創建數值列this.gridView1.CreateColumn("FeeAmount", "費用金額").CreateSpinEdit();//創建備注列this.gridView1.CreateColumn("FeeDescription", "費用說明", 200).CreateMemoEdit();//初始化GridView,可以新增列this.gridView1.InitGridView(GridType.NewItem, false, EditorShowMode.MouseDownFocused, "");//轉義列內容顯示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();}};//對輸入單元格進行非空校驗this.gridView1.ValidateRow += delegate(object sender, ValidateRowEventArgs e){var result = gridControl1.ValidateRowNull(e, new string[]{"FeeType"});};//新增行的內容初始化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 }}}}上面代碼都有詳細的備注,主要就是我們根據數據庫表的關系,創建對應顯示的字段即可,其中有需要隱藏的那么就不要顯示(方便獲取對應的值)
//設置部分列隱藏this.gridView1.CreateColumn("ID", "編號").Visible = false;this.gridView1.CreateColumn("Header_ID", "主表編號").Visible = false;this.gridView1.CreateColumn("Apply_ID", "申請單編號").Visible = false;如果需要綁定下拉列表類似的字段,那么創建對應的數據類型,然后調用綁定函數綁定即可,如下面代碼
//添加下拉列表列,并綁定數據源this.gridView1.CreateColumn("FeeType", "費用類型", 100).CreateComboBox().BindDictItems("費用類型");如果是一些特殊的輸入需要設置格式顯示或者掩碼,那么如下所示
//創建日期列并指定格式var OccurTime = this.gridView1.CreateColumn("OccurTime", "發生時間", 120).CreateDateEdit();OccurTime.EditMask = "yyyy-MM-dd HH:mm";OccurTime.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm";另外有一個值得注意的就是我們新增一行從表記錄的時候,需要記錄一些主表的屬性,這樣的話,我們就是在行初始化的時候,賦值給從表的隱藏列即可。
//新增行的內容初始化 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);}; 在界面中如果我們需要顯示主表的信息,那么就根據條件獲取對應的主表記錄對象,然后顯示給界面控件即可。 /// <summary>/// 顯示常規的對象內容/// </summary>/// <param name="info"></param>private void DisplayInfo(ReimbursementInfo info){tempInfo = info;//重新給臨時對象賦值,使之指向存在的記錄對象 txtCategory.Text = info.Category;txtReason.Text = info.Reason;txtTotalAmount.Value = info.TotalAmount;txtNote.Text = info.Note;}而保存的時候,我們把界面內容重新賦值給對應的主表對象。
/// <summary>/// 編輯或者保存狀態下取值函數/// </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明細輸入的時候,就通過一個函數遍歷獲取GridView的行記錄,轉換為相應的對象即可,如下所示。
/// <summary>/// 獲取明細列表/// </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;}這樣處理完這些信息后,我們就可以在主表保存的時候,同時保存明細表信息即可。
/// <summary>/// 新增狀態下的數據保存/// </summary>/// <returns></returns>public override bool SaveAddNew(){ReimbursementInfo info = tempInfo;//必須使用存在的局部變量,因為部分信息可能被附件使用 SetInfo(info);info.Creator = base.LoginUserInfo.ID;info.CreateTime = DateTime.Now;try{#region 新增數據bool succeed = BLLFactory<Reimbursement>.Instance.Insert(info);if (succeed){//可添加其他關聯操作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);可以對新增記錄保存,也可以對存在的記錄進行更新。
通過上面的介紹,我們可以看到不同的主從表其實邏輯還是很通用的,我們可以把它們的邏輯抽取出來,通過代碼生成工具進行快速生成即可。
本文轉自博客園伍華聰的博客,原文鏈接:Winform界面中主從表編輯界面的快速處理,如需轉載請自行聯系原博主。
總結
以上是生活随笔為你收集整理的Winform界面中主从表编辑界面的快速处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用脚本将文字插入到图片或进行多个图片拼
- 下一篇: 代码生成工具Database2Sharp