Database2Sharp重要更新之生成Winform框架界面代码
本次主要的修改如下:
1)修改设置不重新加载数据库信息。
以前版本,为了保证对数据库基础信息的及时更新,只要对一些关键的参数设置,通常都要重新刷新数据库基础信息(如表、列等基础信息),这样生成的代码就会保证使用最新的设置信息。如上图中的过滤表前缀的参数,是为了把表名称转换为友好名称的类名而定义的,他是在数据库加载的时候,自动把表别名信息生成的。
这个问题使用上一直感觉不太好,7.0版本修复了这个问题,把相关的信息保存的同时,在内存中对数据库表、字段等基础对象进行了更新,确保使用最新配置而无需重新加载整个数据库信息。
2)Winform界面代码可配置生成
说起Winform界面的代码生成,一般来说就两种界面比较典型,一个是查询列表显示界面,一个是数据查看编辑界面。本功能也主要是提供这两类界面代码的生成,通过配置查询列表中的条件字段以及查询列表字段显示信息,就可以合理生成符合我的WInform框架要求的界面代码,查询列表显示界面类继承自BaseDock基础类。另一方面,通过配置数据查看编辑界面的编辑字段,数据检查字段,判断关键数据重复的字段等参数,可以生成较为完善的数据查看编辑界面代码,生成界面如下所示。
以上参数只要执行生成代码一次,即会自动保存起来,下次打开相同表的时候,会把之前的配置信息还原,方便用户的多次操作。由于界面在不同的界面控件(如DevExpress、传统界面等)处理上有所不同,因此生成的界面代码也会根据用户的设置信息进行选择性生成。另外界面代码是我的Winform框架体系里面一部分,所以整合了我的分页控件、基础窗体类的继承等特点,如果你看不懂,可以多看看我的Winform框架文章或者分页控件文章介绍。
Winform界面代码生成后,会直接在代码编辑窗体中打开,用户可以复制或者保存起来放到VS的编辑器中进行相应的修改,后续的工作应该较为轻松了。
下面我们来分别看看生成的代码效果如何吧。
1)查询列表界面类
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using WHC.Pager.Entity;
using WHC.OrderWater.Commons;
using WHC.Dictionary;
using WHC.TestProject.BLL;
using WHC.TestProject.Entity;
namespace WHC.TestProject.UI
{
public partial class FrmItemDetail : BaseDock
{
public FrmItemDetail()
{
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.winGridViewPager1.dataGridView1.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridView1_DataBindingComplete);
}
/// <summary>
/// 绑定数据后,分配各列的宽度
/// </summary>
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
if (this.winGridViewPager1.dataGridView1.Columns.Count > 0)
{
this.winGridViewPager1.dataGridView1.Columns["ID"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["ItemNo"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["ItemName"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["Manufacture"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["MapNo"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["Specification"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["Material"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["ItemBigType"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["ItemType"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["Unit"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["Price"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["Source"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["StoragePos"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["UsagePos"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["Note"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["WareHouse"].Width = 100;
this.winGridViewPager1.dataGridView1.Columns["Dept"].Width = 100;
}
}
/// <summary>
/// 编写初始化窗体的实现,可以用于刷新
/// </summary>
public override void FormOnLoad()
{
BindData();
}
/// <summary>
/// 初始化字典列表内容
/// </summary>
private void InitDictItem()
{
//初始化代码
}
/// <summary>
/// 分页控件刷新操作
/// </summary>
private void winGridViewPager1_OnRefresh(object sender, EventArgs e)
{
BindData();
}
/// <summary>
/// 分页控件删除操作
/// </summary>
private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e)
{
if (MessageUtil.ShowYesNoAndTips("您确定删除选定的记录么?") == DialogResult.No)
{
return;
}
DataGridView grid = this.winGridViewPager1.dataGridView1;
if (grid != null)
{
foreach (DataGridViewRow row in grid.SelectedRows)
{
BLLFactory<ItemDetail>.Instance.Delete(row.Cells["ID"].Value.ToString());
}
}
BindData();
}
/// <summary>
/// 分页控件编辑项操作
/// </summary>
private void winGridViewPager1_OnEditSelected(object sender, EventArgs e)
{
DataGridView grid = this.winGridViewPager1.dataGridView1;
if(grid.SelectedRows.Count == 0) return;
string ID = grid.SelectedRows[0].Cells["ID"].Value.ToString();
List<string> IDList = new List<string>();
if (grid != null)
{
foreach (DataGridViewRow row in grid.Rows)
{
IDList.Add(row.Cells["ID"].Value.ToString());
}
}
if (!string.IsNullOrEmpty(ID))
{
FrmEditItemDetail dlg = new FrmEditItemDetail();
dlg.ID = ID;
dlg.IDList = IDList;
if (DialogResult.OK == dlg.ShowDialog())
{
BindData();
}
}
}
/// <summary>
/// 分页控件新增操作
/// </summary>
private void winGridViewPager1_OnAddNew(object sender, EventArgs e)
{
btnAddNew_Click(null, null);
}
/// <summary>
/// 分页控件全部导出操作前的操作
/// </summary>
private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
{
string where = GetConditionSql();
this.winGridViewPager1.AllToExport = BLLFactory<ItemDetail>.Instance.FindToDataTable(where);
}
/// <summary>
/// 分页控件翻页的操作
/// </summary>
private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
{
BindData();
}
/// <summary>
/// 根据查询条件构造查询语句
/// </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);
string where = condition.BuildConditionSql(DatabaseType.SqlServer).Replace("Where", "");
return where;
}
/// <summary>
/// 绑定列表数据
/// </summary>
private void BindData()
{
this.winGridViewPager1.DisplayColumns = "ID,ItemNo,ItemName,Manufacture,MapNo,Specification,Material,ItemBigType,ItemType,Unit,Price,Source,StoragePos,UsagePos,Note,WareHouse,Dept";
#region 添加别名解析
this.winGridViewPager1.AddColumnAlias("ID", "ID");
this.winGridViewPager1.AddColumnAlias("ItemNo", "备件编号");
this.winGridViewPager1.AddColumnAlias("ItemName", "备件名称");
this.winGridViewPager1.AddColumnAlias("Manufacture", "供货商");
this.winGridViewPager1.AddColumnAlias("MapNo", "图号");
this.winGridViewPager1.AddColumnAlias("Specification", "规格型号");
this.winGridViewPager1.AddColumnAlias("Material", "材质");
this.winGridViewPager1.AddColumnAlias("ItemBigType", "备件属类");
this.winGridViewPager1.AddColumnAlias("ItemType", "备件类别");
this.winGridViewPager1.AddColumnAlias("Unit", "单位");
this.winGridViewPager1.AddColumnAlias("Price", "单价");
this.winGridViewPager1.AddColumnAlias("Source", "来源");
this.winGridViewPager1.AddColumnAlias("StoragePos", "库位");
this.winGridViewPager1.AddColumnAlias("UsagePos", "使用位置");
this.winGridViewPager1.AddColumnAlias("Note", "备注");
this.winGridViewPager1.AddColumnAlias("WareHouse", "所属库房");
this.winGridViewPager1.AddColumnAlias("Dept", "所属部门");
#endregion
string where = GetConditionSql();
List<ItemDetailInfo> list = BLLFactory<ItemDetail>.Instance.Find(where, this.winGridViewPager1.PagerInfo);
this.winGridViewPager1.DataSource = new WHC.Pager.WinControl.SortableBindingList<ItemDetailInfo>(list);
this.winGridViewPager1.PrintTitle = Portal.gc.gAppUnit + " -- " + "信息报表";
}
/// <summary>
/// 查询数据操作
/// </summary>
private void btnSearch_Click(object sender, EventArgs e)
{
BindData();
}
/// <summary>
/// 新增数据操作
/// </summary>
private void btnAddNew_Click(object sender, EventArgs e)
{
FrmEditItemDetail dlg = new FrmEditItemDetail();
if (DialogResult.OK == dlg.ShowDialog())
{
BindData();
}
}
/// <summary>
/// 提供给控件回车执行查询的操作
/// </summary>
private void SearchControl_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
btnSearch_Click(null, null);
}
}
}
}
DevExpress界面效果图
2)数据编辑界面类
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using WHC.Dictionary;
using WHC.OrderWater.Commons;
using WHC.TestProject.BLL;
using WHC.TestProject.Entity;
namespace WHC.TestProject.UI
{
public partial class FrmEditItemDetail : BaseEditForm
{
public FrmEditItemDetail()
{
InitializeComponent();
}
/// <summary>
/// 实现控件输入检查的函数
/// </summary>
/// <returns></returns>
public override bool CheckInput()
{
bool result = true;//默认是可以通过
#region MyRegion
if (this.txtItemNo.Text.Trim().Length == 0)
{
MessageUtil.ShowTips("请输入备件编号");
this.txtItemNo.Focus();
result = false;
}
else if (this.txtItemName.Text.Trim().Length == 0)
{
MessageUtil.ShowTips("请输入备件名称");
this.txtItemName.Focus();
result = false;
}
else if (this.txtManufacture.Text.Trim().Length == 0)
{
MessageUtil.ShowTips("请输入供货商");
this.txtManufacture.Focus();
result = false;
}
#endregion
return result;
}
/// <summary>
/// 初始化数据字典
/// </summary>
private void InitDictItem()
{
//初始化代码
}
/// <summary>
/// 数据显示的函数
/// </summary>
public override void DisplayData()
{
InitDictItem();//数据字典加载(公用)
if (!string.IsNullOrEmpty(ID))
{
#region 显示客户信息
ItemDetailInfo info = BLLFactory<ItemDetail>.Instance.FindByID(ID);
if (info != null)
{
txtItemNo.Text = info.ItemNo;
txtItemName.Text = info.ItemName;
txtManufacture.Text = info.Manufacture;
txtMapNo.Text = info.MapNo;
txtSpecification.Text = info.Specification;
txtMaterial.Text = info.Material;
txtItemBigType.Text = info.ItemBigType;
txtItemType.Text = info.ItemType;
txtUnit.Text = info.Unit;
txtPrice.Text = info.Price;
txtSource.Text = info.Source;
txtStoragePos.Text = info.StoragePos;
txtUsagePos.Text = info.UsagePos;
txtNote.Text = info.Note;
txtWareHouse.Text = info.WareHouse;
txtDept.Text = info.Dept;
}
#endregion
//this.btnOK.Enabled = Portal.gc.HasFunction("ItemDetail/Edit");
}
else
{
//this.btnOK.Enabled = Portal.gc.HasFunction("ItemDetail/Add");
}
}
/// <summary>
/// 编辑或者保存状态下取值函数
/// </summary>
/// <param name="info"></param>
private void SetInfo(ItemDetailInfo info)
{
info.ItemNo = txtItemNo.Text;
info.ItemName = txtItemName.Text;
info.Manufacture = txtManufacture.Text;
info.MapNo = txtMapNo.Text;
info.Specification = txtSpecification.Text;
info.Material = txtMaterial.Text;
info.ItemBigType = txtItemBigType.Text;
info.ItemType = txtItemType.Text;
info.Unit = txtUnit.Text;
info.Price = txtPrice.Text;
info.Source = txtSource.Text;
info.StoragePos = txtStoragePos.Text;
info.UsagePos = txtUsagePos.Text;
info.Note = txtNote.Text;
info.WareHouse = txtWareHouse.Text;
info.Dept = txtDept.Text;
}
/// <summary>
/// 新增状态下的数据保存
/// </summary>
/// <returns></returns>
public override bool SaveAddNew()
{
ItemDetailInfo info = new ItemDetailInfo();
SetInfo(info);
try
{
#region 新增数据
//检查是否还有其他相同关键字的记录
bool exist = BLLFactory<ItemDetail>.Instance.IsExistKey("ItemNo", info.ItemNo);
if (exist)
{
MessageUtil.ShowTips("指定的【备件编号】已经存在,不能重复添加,请修改");
return false;
}
bool succeed = BLLFactory<ItemDetail>.Instance.Insert(info);
if (succeed)
{
//可添加其他关联操作
return true;
}
#endregion
}
catch (Exception ex)
{
LogHelper.Error(ex);
MessageUtil.ShowError(ex.Message);
}
return false;
}
/// <summary>
/// 编辑状态下的数据保存
/// </summary>
/// <returns></returns>
public override bool SaveUpdated()
{
//检查不同ID是否还有其他相同关键字的记录
bool exist = BLLFactory<ItemDetail>.Instance.CheckExist(this.txtItemNo.Text, ID);
if (exist)
{
MessageUtil.ShowTips("指定的【备件编号】已经存在,不能重复添加,请修改");
return false;
}
ItemDetailInfo info = BLLFactory<ItemDetail>.Instance.FindByID(ID);
if (info != null)
{
SetInfo(info);
try
{
#region 更新数据
bool succeed = BLLFactory<ItemDetail>.Instance.Update(info, info.ID.ToString());
if (succeed)
{
//可添加其他关联操作
return true;
}
#endregion
}
catch (Exception ex)
{
LogHelper.Error(ex);
MessageUtil.ShowError(ex.Message);
}
}
return false;
}
}
}
DevExpress界面效果图
Database2Sharp代码生成工具已经上传到服务器,需要该代码生成工具的,请到下载地址(http://www.iqidi.com/download/Database2SharpSetup.rar )下载。
转载请注明出处:撰写人:伍华聪 http://www.iqidi.com