轉帖|使用教程|編輯:龔雪|2020-12-18 13:15:31.813|閱讀 578 次
概述:DevExpress Winforms Controls 內置140多個UI控件和庫,本文將為大家介紹主從表編輯界面的快速處理。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
DevExpress Winforms Controls 內置140多個UI控件和庫,完美構建流暢、美觀且易于使用的應用程序。DevExpress WinForm v20.2全新發(fā)布,想要體驗?點擊下載>>
在Winform開發(fā)中,我們往往除了常規(guī)的單表信息錄入外,有時候設計到多個主從表的數(shù)據(jù)顯示、編輯等界面,單表的信息一般就是控件和對象實體一一對應,然后調用API保存即可,主從表就需要另外特殊處理,本隨筆介紹如何快速實現(xiàn)主從表編輯界面的處理,結合GridControl控件的GridView控件對象,實現(xiàn)數(shù)據(jù)在列表中的實時編輯,非常方便。
主從表一般涉及兩個以上的表,一個是主表,其他的是從表的,在實際情況下,一般包含兩個表較多,我們這里以兩個表的主從表關系進行分析處理。
例如我們建立兩個報銷申請單表關系如下所示。
 
 
對于報銷的主從表信息,我們可以在列表中進行展示,如下界面所示,分為兩部分:一部分是主表信息,一部分是從表信息,單擊主表信息后,顯示對應從表的列表信息。
 
 
那么我們新增一條主表記錄的時候,那么可以彈出一個新的界面進行數(shù)據(jù)的維護處理,方便我們錄入主從表的信息,界面如下所示。
 
 
上面界面包括了主表信息,以及從表的信息(在GridView中實時錄入)兩部分,這樣填寫后統(tǒng)一進行提交處理。
這里主要介紹一下主從表的編輯界面處理,也就是上面這個界面的實現(xiàn)處理。
 
 
其中初始化GridView的代碼如下所示。
/// <summary>
/// 初始化明細表的GridView數(shù)據(jù)顯示
/// </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;
//添加下拉列表列,并綁定數(shù)據(jù)源
this.gridView1.CreateColumn("FeeType", "費用類型", 100).CreateComboBox().BindDictItems("費用類型");
//創(chuàng)建日期列并指定格式
var OccurTime = this.gridView1.CreateColumn("OccurTime", "發(fā)生時間", 120).CreateDateEdit();
OccurTime.EditMask = "yyyy-MM-dd HH:mm";
OccurTime.DisplayFormat.FormatString = "yyyy-MM-dd HH:mm";
//創(chuàng)建數(shù)值列
this.gridView1.CreateColumn("FeeAmount", "費用金額").CreateSpinEdit();
//創(chuàng)建備注列
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
}
}
}
}
上面代碼都有詳細的備注,主要就是我們根據(jù)數(shù)據(jù)庫表的關系,創(chuàng)建對應顯示的字段即可,其中有需要隱藏的那么就不要顯示(方便獲取對應的值)
//設置部分列隱藏
this.gridView1.CreateColumn("ID", "編號").Visible = false;
this.gridView1.CreateColumn("Header_ID", "主表編號").Visible = false;
this.gridView1.CreateColumn("Apply_ID", "申請單編號").Visible = false;
如果需要綁定下拉列表類似的字段,那么創(chuàng)建對應的數(shù)據(jù)類型,然后調用綁定函數(shù)綁定即可,如下面代碼
//添加下拉列表列,并綁定數(shù)據(jù)源
this.gridView1.CreateColumn("FeeType", "費用類型", 100).CreateComboBox().BindDictItems("費用類型");
如果是一些特殊的輸入需要設置格式顯示或者掩碼,那么如下所示
//創(chuàng)建日期列并指定格式
var OccurTime = this.gridView1.CreateColumn("OccurTime", "發(fā)生時間", 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);
};
在界面中如果我們需要顯示主表的信息,那么就根據(jù)條件獲取對應的主表記錄對象,然后顯示給界面控件即可。
/// <summary>
/// 顯示常規(guī)的對象內容
/// </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>
/// 編輯或者保存狀態(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明細輸入的時候,就通過一個函數(shù)遍歷獲取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>
/// 新增狀態(tài)下的數(shù)據(jù)保存
/// </summary>
/// <returns></returns>
public override bool SaveAddNew()
{
ReimbursementInfo info = tempInfo;//必須使用存在的局部變量,因為部分信息可能被附件使用
SetInfo(info);
info.Creator = base.LoginUserInfo.ID;
info.CreateTime = DateTime.Now;
try
{
#region 新增數(shù)據(jù)
bool succeed = BLLFactory<Reimbursement>.Instance.Insert(info);
if (succeed)
{
//可添加其他關聯(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);
可以對新增記錄保存,也可以對存在的記錄進行更新。
通過上面的介紹,我們可以看到不同的主從表其實邏輯還是很通用的,我們可以把它們的邏輯抽取出來,通過代碼生成工具進行快速生成即可。
本文轉載自
DevExpress技術交流群2:775869749 歡迎一起進群討論
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn
文章轉載自: