单表演练之产品资料,辅助组件ViewDataBinding模式
一、数据库新建表 dt_Data_Product
表结构预览:
SQL 语句:
CREATE TABLE [dbo].[dt_Data_Product]( [isid] [INT] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL, [ProductID] [VARCHAR](20) NOT NULL, [Material] [VARCHAR](20) NULL, [ProductName] [VARCHAR](20) NULL, [ZJM] [VARCHAR](20) NULL, [Specification] [NVARCHAR](20) NULL, [Unit] [VARCHAR](10) NULL, [Remark] [NVARCHAR](200) NULL, [CreateUser] [VARCHAR](20) NULL, [CreateDate] [DATETIME] NULL, [LastUpdateUser] [VARCHAR](20) NULL, [LastUpdateDate] [DATETIME] NULL, CONSTRAINT [PK_DT_DATA_PRODUCT] PRIMARY KEY CLUSTERED ( [ProductID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'自增列' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'dt_Data_Product', @level2type=N'COLUMN',@level2name=N'isid' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'产品编号' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'dt_Data_Product', @level2type=N'COLUMN',@level2name=N'ProductID' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'材质' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'dt_Data_Product', @level2type=N'COLUMN',@level2name=N'Material' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'产品名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'dt_Data_Product', @level2type=N'COLUMN',@level2name=N'ProductName' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'助记码' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'dt_Data_Product', @level2type=N'COLUMN',@level2name=N'ZJM' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'规格' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'dt_Data_Product', @level2type=N'COLUMN',@level2name=N'Specification' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'单位' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'dt_Data_Product', @level2type=N'COLUMN',@level2name=N'Unit' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'备注' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'dt_Data_Product', @level2type=N'COLUMN',@level2name=N'Remark' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建人' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'dt_Data_Product', @level2type=N'COLUMN',@level2name=N'CreateUser' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'dt_Data_Product', @level2type=N'COLUMN',@level2name=N'CreateDate' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'最后修改人' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'dt_Data_Product', @level2type=N'COLUMN',@level2name=N'LastUpdateUser' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'最后修改时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'dt_Data_Product', @level2type=N'COLUMN',@level2name=N'LastUpdateDate' GO EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'基础资料-产品资料' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'dt_Data_Product' GO
二、代码生成器生成代码
1)生成model层代码
2)生成窗体代码
使用辅助组件生成模式,窗体会采用 ViewDataBinding 绑定得模式
数据绑定模式,参考: https://www.yesdotnet.com/archive/YESWinform/1628351472.html
生成的代码
3)移动生成代码到项目
移动 Model 代码 ( dt_Data_Product ) 到 YESWinDemo.Models 项目
移动 窗体代码 到项目 YESWinDemo.Dictionary
3)创建BLL类
完成第二步后,窗体 frm_Product 中会有一个报错
这里需要手动在 YESWinDemo.Business 中创建一个 BLL_Product 类
代码如下:
using YESWinDemo.Models; namespace YESWinDemo.Business.Data { public class BLL_Product : Base.bllBaseUser { public BLL_Product() : base(typeof(dt_Data_Product)) { } } }
三、功能注册
在模块的 Management.cs 中注册功能
FunctionCollection.AddFunction(typeof(frm_Product), "产品资料", "Function_Product");
四、运行测试
略
五、LookUpEdit 公共字典
材质希望从公共字典中获取,公共字典扩展方法参考 https://www.yesdotnet.com/archive/YESWinform/1628347364.html
1)更改 材质 控件为 LookUpEdit
选中控件,点击右上角的小三角图标,弹窗中选择要切换的控件类型( LookUpEdit )
数据编辑页面也要替换,界面布局参考: https://www.yesdotnet.com/archive/YESWinform/1628349895.html
更改控件类型后,要重新设置 ValidateForSave
属性 和 ViewDataBinding
属性
ValidateForSave 属性,用于保存前数据校验
ViewDataBinding 属性,用户和数据源进行绑定,如果不设置该值,更改了控件值,数据源不会更新
更多详细资料,参考: https://www.yesdotnet.com/archive/YESWinform/1628351472.html
2)绑定控件数据源
在窗体 Load 事件中添加绑定材质数据源事件
Library.DataBinderTools.Bound.BoundCommonDictDataName(txtMaterial, Business.CustomerEnum.EnumCommonDicData.产品材质, false, false); Library.DataBinderTools.Bound.BoundCommonDictDataName(txts_Material, Business.CustomerEnum.EnumCommonDicData.产品材质, false, true);
注意:
txts_Material 控件时检索控件,所以绑定数据源中第四个参数为true,会添加一个空的选择
区别,左边为 txts_material 右边为 txtMaterial
重复 1 和 2 步骤,给设置单位的数据源,也来自于 公共字典
六、效果展示