講解winform下拉框綁定數據:winform下拉框控制項

在Winform開發中,我們往往除了常規的單表信息錄入外,有時候設計到多個主從表的數據顯示、編輯等界面,單表的信息一般就是控制項和對象實體一一對應,然後調用API保存即可,主從表就需要另外特殊處理,本隨筆介紹如何快速實現主從表編輯界面的處理,結合GridControl控制項的GridView控制項對象,實現數據在列表中的實時編輯,非常方便。

一、主從表的界面設計及展示

主從表一般涉及兩個以上的表,一個是主表,其他的是從表的,在實際情況下,一般包含兩個表較多,我們這裡以兩個表的主從表關係進行分析處理。

例如我們建立兩個報銷申請單表關係如下所示。

「應用界面優化」Winform開發主從表編輯界面的快速處理

對於報銷的主從表信息,我們可以在列表中進行展示,如下界面所示,分為兩部分:一部分是主表信息,一部分是從表信息,單擊主表信息後,顯示對應從表的列表信息。

「應用界面優化」Winform開發主從表編輯界面的快速處理

那麼我們新增一條主表記錄的時候,那麼可以彈出一個新的界面進行數據的維護處理,方便我們錄入主從表的信息,界面如下所示。

「應用界面優化」Winform開發主從表編輯界面的快速處理

上面界面包括了主表信息,以及從表的信息(在GridView中實時錄入)兩部分,這樣填寫後統一進行提交處理。

二、主從表編輯界面的處理

這裡主要介紹一下主從表的編輯界面處理,也就是上面這個界面的實現處理。

「應用界面優化」Winform開發主從表編輯界面的快速處理

其中初始化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);

可以對新增記錄保存,也可以對存在的記錄進行更新。

通過上面的介紹,我們可以看到不同的主從表其實邏輯還是很通用的,我們可以把它們的邏輯抽取出來,通過代碼生成工具進行快速生成即可。

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/223137.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-09 14:14
下一篇 2024-12-09 14:14

相關推薦

發表回復

登錄後才能評論