代碼生成工具之Winform查詢列表界面生成
在上面一篇隨筆《代碼生成工具之界面快速生成》介紹了代碼生成工具Database2Sharp的界面生成操作,其中介紹了Web界面(包括列表界面、內(nèi)容顯示、內(nèi)容編輯界面的生成,另外還介紹了Winform界面的內(nèi)容編輯界面的生成,本篇主要繼續(xù)介紹Winform界面生成中的查詢列表界面的操作展示等信息。
基于Winform的界面生成,配合我的Winform開發(fā)框架,提供了三種不同的界面生成,包括傳統(tǒng)界面樣式、DotNetBar界面樣式和DevExpress界面樣式的代碼生成,這幾種界面是目前Winform開發(fā)中非常常見的界面樣式。
1、EnterpriseLibray架構(gòu)代碼及Web界面生成
基于EnterpriseLibray架構(gòu)的代碼生成,除了可以生成傳統(tǒng)的業(yè)務(wù)層、數(shù)據(jù)訪問層、數(shù)據(jù)訪問接口層、實(shí)體層外,還同時(shí)生成了Web界面工程,Web界面工程已經(jīng)添加了相關(guān)的引用程序集及必須的控件,生成后即可編譯運(yùn)行,一些表字段設(shè)計(jì)合理的話,甚至不用修改一行代碼就能直接使用,如下所示。

生成的Web界面截圖如下所示,包含列表查詢界面、內(nèi)容顯示界面、內(nèi)容編輯界面等。

內(nèi)容編輯界面(新增和編輯)如下所示。

2、Winform界面生成
1)Winform數(shù)據(jù)編輯界面生成
在上面一篇隨筆《代碼生成工具之界面快速生成》同時(shí)也介紹了Winform界面的生成,上一篇主要介紹了Winform界面中的數(shù)據(jù)編輯界面。
Winform界面生成界面提供了很多參數(shù)進(jìn)行控制,以期生成精細(xì)化的界面內(nèi)容。

DevExpress界面樣式的生成。

傳統(tǒng)樣式的界面生成。

2)Winform查詢列表界面生成
很多情況下,查詢列表界面很常見,如果能快速生成標(biāo)準(zhǔn)的界面,除了可以節(jié)省時(shí)間,提高開發(fā)效率外,也給我們統(tǒng)一界面風(fēng)格及代碼風(fēng)格等方面,提供更好的支持。
復(fù)雜累贅的界面能夠自動(dòng)生成,絕對(duì)是開發(fā)過程的一大提升,讓我們更加享受開發(fā)的樂趣。
1)設(shè)置好相關(guān)的界面參數(shù),如指定列表的查詢字段、列表顯示字段,選擇界面樣式,以及設(shè)定代碼的主命名空間等參數(shù)。

2)生成界面代碼到文件后,把文件直接復(fù)制到項(xiàng)目中,不用修改直接就可以看到列表界面效果,Yeah,正是我們需要的樣式。

當(dāng)然,上面的查詢條件可以一行,也可以兩行或者多行,界面生成的時(shí)候,會(huì)自動(dòng)合理計(jì)算好布局,保證完美展現(xiàn)我們想要的列表界面效果。以上界面幾乎不用任何修改就直接可以編譯運(yùn)行,里面的后臺(tái)代碼也同時(shí)生成了。
后臺(tái)代碼里面生成包括,分頁(yè)控件展示及列表查詢顯示、Excel數(shù)據(jù)導(dǎo)入、數(shù)據(jù)導(dǎo)出等功能的后臺(tái)界面代碼。以下就是完整生成的界面后臺(tái)代碼,沒有編輯過的列表界面后臺(tái)代碼如下所示。
public partial class FrmItemDetail : BaseDock { public FrmItemDetail2() { InitializeComponent(); InitDictItem(); this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged); this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport); this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected); this.winGridViewPager1.OnAddNew += new EventHandler(winGridViewPager1_OnAddNew); this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected); this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh); this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1; this.winGridViewPager1.ShowLineNumber = true; this.txtItemNo.KeyUp += new System.Windows.Forms.KeyEventHandler(this.SearchControl_KeyUp); this.txtItemName.KeyUp += new System.Windows.Forms.KeyEventHandler(this.SearchControl_KeyUp); this.txtManufacture.KeyUp += new System.Windows.Forms.KeyEventHandler(this.SearchControl_KeyUp); this.txtMapNo.KeyUp += new System.Windows.Forms.KeyEventHandler(this.SearchControl_KeyUp); this.txtSpecification.KeyUp += new System.Windows.Forms.KeyEventHandler(this.SearchControl_KeyUp); this.txtMaterial.KeyUp += new System.Windows.Forms.KeyEventHandler(this.SearchControl_KeyUp); this.txtItemBigType.KeyUp += new System.Windows.Forms.KeyEventHandler(this.SearchControl_KeyUp); this.txtItemType.KeyUp += new System.Windows.Forms.KeyEventHandler(this.SearchControl_KeyUp); this.txtUnit.KeyUp += new System.Windows.Forms.KeyEventHandler(this.SearchControl_KeyUp); this.txtSource.KeyUp += new System.Windows.Forms.KeyEventHandler(this.SearchControl_KeyUp); } /// <summary> /// 編寫初始化窗體的實(shí)現(xiàn),可以用于刷新 /// </summary> public override void FormOnLoad() { BindData(); } /// <summary> /// 初始化字典列表內(nèi)容 /// </summary> private void InitDictItem() { //初始化代碼 } /// <summary> /// 分頁(yè)控件刷新操作 /// </summary> private void winGridViewPager1_OnRefresh(object sender, EventArgs e) { BindData(); } /// <summary> /// 分頁(yè)控件刪除操作 /// </summary> private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e) { if (MessageDxUtil.ShowYesNoAndTips("您確定刪除選定的記錄么?") == DialogResult.No) { return; } int[] rowSelected = this.winGridViewPager1.GridView1.GetSelectedRows(); foreach (int iRow in rowSelected) { string ID = this.winGridViewPager1.GridView1.GetRowCellDisplayText(iRow, "ID"); BLLFactory<ItemDetail>.Instance.Delete(ID); } BindData(); } /// <summary> /// 分頁(yè)控件編輯項(xiàng)操作 /// </summary> private void winGridViewPager1_OnEditSelected(object sender, EventArgs e) { string ID = this.winGridViewPager1.gridView1.GetFocusedRowCellDisplayText("ID"); List<string> IDList = new List<string>(); for (int i = 0; i < this.winGridViewPager1.gridView1.RowCount; i++) { string strTemp = this.winGridViewPager1.GridView1.GetRowCellDisplayText(i, "ID"); IDList.Add(strTemp); } if (!string.IsNullOrEmpty(ID)) { FrmEditItemDetail dlg = new FrmEditItemDetail(); dlg.ID = ID; dlg.IDList = IDList; if (DialogResult.OK == dlg.ShowDialog()) { BindData(); } } } /// <summary> /// 分頁(yè)控件新增操作 /// </summary> private void winGridViewPager1_OnAddNew(object sender, EventArgs e) { btnAddNew_Click(null, null); } /// <summary> /// 分頁(yè)控件全部導(dǎo)出操作前的操作 /// </summary> private void winGridViewPager1_OnStartExport(object sender, EventArgs e) { string where = GetConditionSql(); this.winGridViewPager1.AllToExport = BLLFactory<ItemDetail>.Instance.FindToDataTable(where); } /// <summary> /// 分頁(yè)控件翻頁(yè)的操作 /// </summary> private void winGridViewPager1_OnPageChanged(object sender, EventArgs e) { BindData(); } /// <summary> /// 根據(jù)查詢條件構(gòu)造查詢語句 /// </summary> private string GetConditionSql() { SearchCondition condition = new SearchCondition(); condition.AddCondition("ItemNo", this.txtItemNo.Text, SqlOperator.Like); condition.AddCondition("ItemName", this.txtItemName.Text, SqlOperator.Like); condition.AddCondition("Manufacture", this.txtManufacture.Text, SqlOperator.Like); condition.AddCondition("MapNo", this.txtMapNo.Text, SqlOperator.Like); condition.AddCondition("Specification", this.txtSpecification.Text, SqlOperator.Like); condition.AddCondition("Material", this.txtMaterial.Text, SqlOperator.Like); condition.AddCondition("ItemBigType", this.txtItemBigType.Text, SqlOperator.Like); condition.AddCondition("ItemType", this.txtItemType.Text, SqlOperator.Like); condition.AddCondition("Unit", this.txtUnit.Text, SqlOperator.Like); condition.AddCondition("Source", this.txtSource.Text, SqlOperator.Like); string where = condition.BuildConditionSql(DatabaseType.SqlServer).Replace("Where", ""); return where; } /// <summary> /// 綁定列表數(shù)據(jù) /// </summary> private void BindData() { this.winGridViewPager1.DisplayColumns = "ItemNo,ItemName,Manufacture,MapNo,Specification,Material,ItemBigType,ItemType,Unit,Price,Source,StoragePos,UsagePos,Note,WareHouse,Dept"; #region 添加別名解析 this.winGridViewPager1.AddColumnAlias("ItemNo", "備件編號(hào)"); this.winGridViewPager1.AddColumnAlias("ItemName", "備件名稱"); this.winGridViewPager1.AddColumnAlias("Manufacture", "供貨商"); this.winGridViewPager1.AddColumnAlias("MapNo", "圖號(hào)"); this.winGridViewPager1.AddColumnAlias("Specification", "規(guī)格型號(hào)"); this.winGridViewPager1.AddColumnAlias("Material", "材質(zhì)"); this.winGridViewPager1.AddColumnAlias("ItemBigType", "備件屬類"); this.winGridViewPager1.AddColumnAlias("ItemType", "備件類別"); this.winGridViewPager1.AddColumnAlias("Unit", "單位"); this.winGridViewPager1.AddColumnAlias("Price", "單價(jià)"); this.winGridViewPager1.AddColumnAlias("Source", "來源"); this.winGridViewPager1.AddColumnAlias("StoragePos", "庫(kù)位"); this.winGridViewPager1.AddColumnAlias("UsagePos", "使用位置"); this.winGridViewPager1.AddColumnAlias("Note", "備注"); this.winGridViewPager1.AddColumnAlias("WareHouse", "所屬庫(kù)房"); this.winGridViewPager1.AddColumnAlias("Dept", "所屬部門"); #endregion string where = GetConditionSql(); List<ItemDetailInfo> list = BLLFactory<ItemDetail>.Instance.FindWithPager(where, this.winGridViewPager1.PagerInfo); this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<ItemDetailInfo>(list); this.winGridViewPager1.PrintTitle = Portal.gc.gAppUnit + " -- " + "信息報(bào)表"; } /// <summary> /// 查詢數(shù)據(jù)操作 /// </summary> private void btnSearch_Click(object sender, EventArgs e) { BindData(); } /// <summary> /// 新增數(shù)據(jù)操作 /// </summary> private void btnAddNew_Click(object sender, EventArgs e) { FrmEditItemDetail dlg = new FrmEditItemDetail(); if (DialogResult.OK == dlg.ShowDialog()) { BindData(); } } /// <summary> /// 提供給控件回車執(zhí)行查詢的操作 /// </summary> private void SearchControl_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { btnSearch_Click(null, null); } } private string moduleName = "備件信息"; /// <summary> /// 導(dǎo)入Excel的操作 /// </summary> private void btnImport_Click(object sender, EventArgs e) { string templateFile = string.Format("{0}-模板.xls", moduleName); FrmImportExcelData dlg = new FrmImportExcelData(); dlg.SetTemplate(templateFile, System.IO.Path.Combine(Application.StartupPath, templateFile)); dlg.OnDataSave += new FrmImportExcelData.SaveDataHandler(ExcelData_OnDataSave); dlg.OnRefreshData += new EventHandler(ExcelData_OnRefreshData); dlg.ShowDialog(); } void ExcelData_OnRefreshData(object sender, EventArgs e) { BindData(); } bool ExcelData_OnDataSave(DataRow dr) { bool success = false; ItemDetailInfo info = new ItemDetailInfo(); info.ItemNo = dr["備件編號(hào)"].ToString(); info.ItemName = dr["備件名稱"].ToString(); info.Manufacture = dr["供貨商"].ToString(); info.MapNo = dr["圖號(hào)"].ToString(); info.Specification = dr["規(guī)格型號(hào)"].ToString(); info.Material = dr["材質(zhì)"].ToString(); info.ItemBigType = dr["備件屬類"].ToString(); info.ItemType = dr["備件類別"].ToString(); info.Unit = dr["單位"].ToString(); info.Price = Convert.ToDecimal(dr["單價(jià)"].ToString()); info.Source = dr["來源"].ToString(); info.StoragePos = dr["庫(kù)位"].ToString(); info.UsagePos = dr["使用位置"].ToString(); info.Note = dr["備注"].ToString(); info.WareHouse = dr["所屬庫(kù)房"].ToString(); info.Dept = dr["所屬部門"].ToString(); success = BLLFactory<ItemDetail>.Instance.Insert(info); return success; } /// <summary> /// 導(dǎo)出Excel的操作 /// </summary> private void btnExport_Click(object sender, EventArgs e) { string file = FileDialogHelper.SaveExcel(string.Format("{0}.xls", moduleName)); if (!string.IsNullOrEmpty(file)) { List<ItemDetailInfo> list = BLLFactory<ItemDetail>.Instance.GetAll(); DataTable dtNew = DataTableHelper.CreateTable("序號(hào)|int"); DataRow dr; int j = 1; for (int i = 0; i < list.Count; i++) { dr = dtNew.NewRow(); dr["序號(hào)"] = j++; dr["備件編號(hào)"] = list[i].ItemNo; dr["備件名稱"] = list[i].ItemName; dr["供貨商"] = list[i].Manufacture; dr["圖號(hào)"] = list[i].MapNo; dr["規(guī)格型號(hào)"] = list[i].Specification; dr["材質(zhì)"] = list[i].Material; dr["備件屬類"] = list[i].ItemBigType; dr["備件類別"] = list[i].ItemType; dr["單位"] = list[i].Unit; dr["單價(jià)"] = list[i].Price; dr["來源"] = list[i].Source; dr["庫(kù)位"] = list[i].StoragePos; dr["使用位置"] = list[i].UsagePos; dr["備注"] = list[i].Note; dr["所屬庫(kù)房"] = list[i].WareHouse; dr["所屬部門"] = list[i].Dept; dtNew.Rows.Add(dr); } try { string error = ""; AsposeExcelTools.DataTableToExcel2(dtNew, file, out error); if (!string.IsNullOrEmpty(error)) { MessageDxUtil.ShowError(string.Format("導(dǎo)出Excel出現(xiàn)錯(cuò)誤:{0}", error)); } else { if (MessageDxUtil.ShowYesNoAndTips("導(dǎo)出成功,是否打開文件?") == System.Windows.Forms.DialogResult.Yes) { System.Diagnostics.Process.Start(file); } } } catch (Exception ex) { LogHelper.Error(ex); MessageDxUtil.ShowError(ex.Message); } } } }
傳統(tǒng)界面效果生成操作,只需要選擇傳統(tǒng)界面樣式,生成即可,得到的初始化界面如下所示。

如果是只是指定了幾個(gè)查詢條件(一行的情況),那么工具會(huì)自動(dòng)計(jì)算好布局位置,得到界面效果如下所示。

代碼生成工具Database2Sharp還提供了生成基于DotNetbar的樣式的查詢列表界面代碼,和上圖類似,在此不再贅述。
通過代碼生成工具,不僅可以生成整體性的EnterpriseLibary框架結(jié)構(gòu)代碼,連我們繁瑣的Web界面、Winform界面都能快速、高標(biāo)準(zhǔn)生成,開發(fā)界面其實(shí)是一件很輕松快速的事情,不要整天從數(shù)據(jù)庫(kù)字段和界面屬性設(shè)置中來回切換了。有了Database2Sharp代碼生成工具,一切變得寧?kù)o很多,但更加快速。
專注于代碼生成工具、.Net/Python 框架架構(gòu)及軟件開發(fā),以及各種Vue.js的前端技術(shù)應(yīng)用。著有Winform開發(fā)框架/混合式開發(fā)框架、微信開發(fā)框架、Bootstrap開發(fā)框架、ABP開發(fā)框架、SqlSugar開發(fā)框架、Python開發(fā)框架等框架產(chǎn)品。
??轉(zhuǎn)載請(qǐng)注明出處:撰寫人:伍華聰??http://www.iqidi.com?
????
posted on 2012-09-24 16:01 伍華聰 閱讀(20263) 評(píng)論(22) 收藏 舉報(bào)
浙公網(wǎng)安備 33010602011771號(hào)