#region using System; using System.Data; using System.Windows.Forms; using CSFrameworkV5.Business; using CSFrameworkV5.CodeGeneratorCore; using CSFrameworkV5.Models; using DevExpress.XtraEditors; using DevExpress.XtraEditors.Controls; using DevExpress.XtraEditors.Repository; using DevExpress.XtraTreeList.Columns; using DataTableTools = CSFrameworkV5.Common.DataTableTools; using Msg = CSFrameworkV5.Common.Msg; #endregion namespace CSFrameworkV5.Library.CommonClass { public class DataBinderToolBase { [CG_LookupDataBoundAttribute("设置勾选组件(CheckEdit)", CG_EditorType.NormalEditor)] public static void BoundCheckEdit(CheckEdit chk) { chk.Properties.AutoHeight = false; chk.Properties.NullStyle = StyleIndeterminate.Unchecked; chk.Properties.ValueChecked = "Y"; chk.Properties.ValueUnchecked = "N"; } [CG_LookupDataBoundAttribute("设置勾选组件(RepositoryItem)", CG_EditorType.RepositoryItemEditor)] public static void BoundCheckEdit(RepositoryItemCheckEdit lueCheck) { lueCheck.AutoHeight = false; lueCheck.NullStyle = StyleIndeterminate.Unchecked; lueCheck.ValueChecked = "Y"; lueCheck.ValueUnchecked = "N"; } /// /// 初始化控件列(Names和FileNames必须保证长度一致) /// /// /// 显示列名集合 /// 对应字段名 protected static void InitializeControl(LookUpEdit lue, string[] Names, string[] FileNames) { try { lue.Properties.Columns.Clear(); lue.Properties.NullText = ""; lue.Properties.DropDownRows = 25; if (lue.Properties.Columns.Count == 0) for (var i = 0; i < Names.Length; i++) { var col = new LookUpColumnInfo(); col.Caption = Names[i]; col.FieldName = FileNames[i]; lue.Properties.Columns.Add(col); } } catch (Exception e) { Msg.Warning(e.Message); } } /// /// 初始化控件列(Names和FileNames必须保证长度一致) /// /// /// 显示列名集合 /// 对应字段名 protected static void InitializeControl(RepositoryItemLookUpEdit lue, string[] Names, string[] FileNames) { try { lue.Columns.Clear(); lue.NullText = ""; if (lue.Columns.Count == 0) for (var i = 0; i < Names.Length; i++) { var col = new LookUpColumnInfo(); col.Caption = Names[i]; col.FieldName = FileNames[i]; lue.Columns.Add(col); } } catch (Exception e) { Msg.Warning(e.Message); } } /// /// 初始化控件列(Names和FileNames必须保证长度一致) /// /// /// 显示列名集合 /// 对应字段名 protected static void InitializeControl( RepositoryItemSearchLookUpEdit lue, string[] Cations, string[] FileName) { try { lue.View.Columns.Clear(); lue.NullText = ""; if (lue.View.Columns.Count == 0) for (var i = 0; i < Cations.Length; i++) { var col = lue.View.Columns.Add(); col.Caption = Cations[i]; col.FieldName = FileName[i]; col.VisibleIndex = lue.View.Columns.Count; } } catch (Exception e) { Msg.Warning(e.Message); } } /// /// 设置下拉窗体显示的行数 /// /// protected static void SetDropDownRows(LookUpEdit lue) { if (lue.Properties.DataSource is DataTable) { var rows = (lue.Properties.DataSource as DataTable).Rows.Count; if (rows < 20) lue.Properties.DropDownRows = rows + 1; else lue.Properties.DropDownRows = 20 + 1; } } protected static void SetDropDownRows(RepositoryItemLookUpEdit lue) { if (lue.DataSource is DataTable) { var rows = (lue.DataSource as DataTable).Rows.Count; if (rows < 20) lue.DropDownRows = rows; else lue.DropDownRows = 20; } } } /// /// 绑定下拉数据组件数据源的工具 /// public class DataBinderTools : DataBinderToolBase { #region 绑定下拉数据组件的数据源 /// /// 绑定组织机构 /// /// TreeListLookUpEdit组件 /// 图标 public static void BoundOrganization(TreeListLookUpEdit lue, ImageList imageList) { //创建TreeList的列 var col_Popup_GroupName = new TreeListColumn(); col_Popup_GroupName.Caption = "组织机构"; col_Popup_GroupName.FieldName = tb_MyGroup.GroupName; col_Popup_GroupName.Name = "col_Popup_GroupName"; col_Popup_GroupName.Visible = true; col_Popup_GroupName.VisibleIndex = 0; col_Popup_GroupName.Width = 130; //TreeListLookUpEdit.Properties lue.Properties.NullText = ""; lue.Properties.DisplayMember = tb_MyGroup.GroupName; lue.Properties.ValueMember = tb_MyGroup.GroupCode; var tree = lue.Properties.TreeList; tree.StateImageList = imageList; tree.Columns.Clear(); tree.Columns.AddRange(new[] { col_Popup_GroupName }); var treeData = DataDictCache.Cache.UserGroup; tree.KeyFieldName = tb_MyGroup.GroupCode; //设置主键 tree.ParentFieldName = tb_MyGroup.ParentGroupCode; //设置父级主键 tree.RootValue = ""; //顶级树结点的值 tree.DataSource = treeData.Copy(); DevStyle.SetTreeListSelectStyle(tree); DevTreeListView.SetImageIndex(tree, null, 1, 0); tree.ExpandAll(); } [CG_LookupDataBoundAttribute("绑定组织机构", CG_EditorType.NormalEditor)] public static void BoundOrganization(LookUpEdit lue, bool AddNull) { lue.Properties.DropDownRows = 15; InitializeControl(lue, new[] { "编号", "组织机构名称" }, new[] { tb_MyGroup.GroupCode, tb_MyGroup.GroupName }); lue.Properties.Columns[0].Width = 80; lue.Properties.Columns[1].Width = 350; lue.Properties.PopupWidth = 430; var dt = DataDictCache.Cache.UserGroup.Copy(); if (AddNull) DataTableTools.AddFirstEmptyRow(dt); DataBinder.BindingLookupEditDataSource(lue, dt, tb_MyGroup.GroupName, tb_MyGroup.GroupCode); SetDropDownRows(lue); } [CG_LookupDataBoundAttribute("绑定组织机构", CG_EditorType.RepositoryItemEditor)] public static void BoundOrganization(RepositoryItemLookUpEdit lue) { lue.DropDownRows = 15; InitializeControl(lue, new[] { "编号", "组织机构名称" }, new[] { tb_MyGroup.GroupCode, tb_MyGroup.GroupName }); lue.Columns[0].Width = 80; lue.Columns[1].Width = 350; lue.PopupWidth = 430; var dt = DataDictCache.Cache.UserGroup.Copy(); DataBinder.BindingLookupEditDataSource(lue, dt, tb_MyGroup.GroupName, tb_MyGroup.GroupCode); SetDropDownRows(lue); } /// /// 绑定表格单元格中用户下拉数据源,tb_MyUser表 /// /// [CG_LookupDataBoundAttribute("绑定用户资料", CG_EditorType.RepositoryItemEditor)] public static void BoundUser(RepositoryItemLookUpEdit lue) { lue.Columns.Clear(); InitializeControl(lue, new[] { "姓名" }, new[] { tb_MyUser.UserName }); lue.DropDownRows = 25; var dt = DataDictCache.Cache.User; //数据源 DataBinder.BindingLookupEditDataSource(lue, dt, tb_MyUser.UserName, tb_MyUser.Account); SetDropDownRows(lue); } /// /// 绑定用户下拉数据源,tb_MyUser表 /// /// [CG_LookupDataBoundAttribute("绑定用户资料", CG_EditorType.NormalEditor)] public static void BoundUser(LookUpEdit lue) { lue.Properties.NullText = ""; lue.Properties.DropDownRows = 15; InitializeControl(lue, new[] { "帐号", "姓名" }, new[] { tb_MyUser.Account, tb_MyUser.UserName }); lue.Properties.Columns[0].Width = 60; lue.Properties.Columns[1].Width = 140; lue.Properties.PopupWidth = 200; var dt = DataDictCache.Cache.User; //数据源 DataBinder.BindingLookupEditDataSource(lue, dt, tb_MyUser.UserName, tb_MyUser.Account); SetDropDownRows(lue); } [CG_LookupDataBoundAttribute("绑定用户资料(第一条记录添加空行)", CG_EditorType.NormalEditor)] public static void BoundUser(LookUpEdit lue, bool firstRowEmpty) { lue.Properties.NullText = ""; lue.Properties.DropDownRows = 15; InitializeControl(lue, new[] { "帐号", "姓名" }, new[] { "Account", "UserName" }); lue.Properties.Columns[0].Width = 60; lue.Properties.Columns[1].Width = 140; lue.Properties.PopupWidth = 200; if (firstRowEmpty) { var dt = DataDictCache.Cache.User.Copy(); DataTableTools.AddFirstEmptyRow(dt); DataBinder.BindingLookupEditDataSource(lue, dt, "UserName", "Account"); } else { DataBinder.BindingLookupEditDataSource(lue, DataDictCache.Cache.User.Copy(), "UserName", "Account"); } SetDropDownRows(lue); } public static void BoundFormTable(LookUpEdit lue) { lue.Properties.DropDownItemHeight = 20; lue.Properties.Columns.Clear(); InitializeControl(lue, new[] { "名称", "编号" }, new[] { sys_BusinessTables.FormCaption, sys_BusinessTables.Table1 }); lue.Properties.Columns[0].Width = 150; lue.Properties.Columns[1].Width = 200; lue.Properties.PopupWidth = 350; var dt = DataDictCache.Cache.BusinessTables; DataBinder.BindingLookupEditDataSource(lue, dt, sys_BusinessTables.FormCaption, sys_BusinessTables.Table1); SetDropDownRows(lue); } /// /// 绑定账套 /// /// /// /// 仅显示可见账套 public static void BoundDBDataSet(LookUpEdit lue, string keyFieldName, bool onlyVisibleDataset = true) { lue.Properties.DropDownItemHeight = 20; lue.Properties.Columns.Clear(); InitializeControl(lue, new[] { "名称", "编号" }, new[] { tb_DataSet.DataSetName, keyFieldName }); lue.Properties.Columns[0].Width = 150; lue.Properties.Columns[1].Width = 200; lue.Properties.PopupWidth = 350; var dt = DataDictCache.Cache.DataSet.Copy(); dt.DefaultView.RowFilter = onlyVisibleDataset ? "IsVisible='Y'" : ""; dt = dt.DefaultView.ToTable(); DataBinder.BindingLookupEditDataSource(lue, dt, tb_DataSet.DataSetName, keyFieldName); SetDropDownRows(lue); } public static void BoundFormTable(RepositoryItemLookUpEdit lue) { lue.DropDownItemHeight = 20; lue.Columns.Clear(); InitializeControl(lue, new[] { "名称", "编号" }, new[] { sys_BusinessTables.FormCaption, sys_BusinessTables.Table1 }); var dt = DataDictCache.Cache.BusinessTables; DataBinder.BindingLookupEditDataSource(lue, dt, sys_BusinessTables.FormCaption, sys_BusinessTables.Table1); SetDropDownRows(lue); } public static void BoundDBDataSet(RepositoryItemLookUpEdit lue) { lue.DropDownItemHeight = 20; lue.Columns.Clear(); InitializeControl(lue, new[] { "名称", "编号" }, new[] { tb_DataSet.DataSetName, tb_DataSet.DBName }); var dt = DataDictCache.Cache.DataSet; DataBinder.BindingLookupEditDataSource(lue, dt, tb_DataSet.DataSetName, tb_DataSet.DBName); SetDropDownRows(lue); } [CG_LookupDataBoundAttribute("设置勾选组件(CheckEdit)", CG_EditorType.NormalEditor)] public static void BoundCheckEdit(CheckEdit chk) { chk.Properties.AutoHeight = false; chk.Properties.NullStyle = StyleIndeterminate.Unchecked; chk.Properties.ValueChecked = "Y"; chk.Properties.ValueUnchecked = "N"; } [CG_LookupDataBoundAttribute("设置勾选组件(RepositoryItem)", CG_EditorType.RepositoryItemEditor)] public static void BoundCheckEdit(RepositoryItemCheckEdit lueCheck) { lueCheck.AutoHeight = false; lueCheck.NullStyle = StyleIndeterminate.Unchecked; lueCheck.ValueChecked = "Y"; lueCheck.ValueUnchecked = "N"; } /// /// 绑定销售员,tb_MyUser表 /// /// /// true:表示增加空行,适用于条件选择, 预设为false [CG_LookupDataBoundAttribute("绑定销售员", CG_EditorType.NormalEditor)] public static void BoundSales(LookUpEdit lue) { lue.Properties.Columns.Clear(); InitializeControl(lue, new[] { "姓名" }, new[] { tb_MyUser.UserName }); lue.Properties.DropDownRows = 25; var dt = DataDictCache.Cache.User.Copy(); DataBinder.BindingLookupEditDataSource(lue, dt, tb_MyUser.UserName, tb_MyUser.Account); SetDropDownRows(lue); } /// /// 绑定销售员,tb_MyUser表 /// /// /// true:表示增加空行,适用于条件选择, 预设为false [CG_LookupDataBoundAttribute("绑定销售员 - 带参数", CG_EditorType.NormalEditor)] public static void BoundSales(LookUpEdit lue, bool AddNull) { lue.Properties.Columns.Clear(); InitializeControl(lue, new[] { "姓名" }, new[] { tb_MyUser.UserName }); lue.Properties.DropDownRows = 25; var dt = DataDictCache.Cache.User.Copy(); if (AddNull) DataTableTools.AddFirstEmptyRow(dt); DataBinder.BindingLookupEditDataSource(lue, dt, tb_MyUser.UserName, tb_MyUser.Account); SetDropDownRows(lue); } /// /// 绑定业务员数据员,tb_MyUser表 /// /// [CG_LookupDataBoundAttribute("绑定销售员", CG_EditorType.RepositoryItemEditor)] public static void BoundSales(RepositoryItemLookUpEdit lue) { lue.Columns.Clear(); InitializeControl(lue, new[] { "姓名" }, new[] { tb_MyUser.UserName }); lue.DropDownRows = 25; var dt = DataDictCache.Cache.User; DataBinder.BindingLookupEditDataSource(lue, dt, tb_MyUser.UserName, tb_MyUser.Account); SetDropDownRows(lue); } /// /// 绑定枚举类型 /// /// /// public static void BoundEnum(RepositoryItemComboBox combo, Type enumType) { var names = Enum.GetNames(enumType); combo.Items.Clear(); combo.Items.AddRange(names); } /// /// 绑定枚举类型 /// /// /// /// 第一行添加空行 public static void BoundEnum(RepositoryItemComboBox combo, Type enumType, bool AddEmptyItem) { var names = Enum.GetNames(enumType); combo.Items.Clear(); combo.Items.AddRange(names); if (AddEmptyItem) combo.Items.Insert(0, ""); } /// /// 绑定枚举类型 /// /// /// public static void BoundEnum(ComboBoxEdit combo, Type enumType) { var names = Enum.GetNames(enumType); combo.Properties.Items.Clear(); combo.Properties.Items.AddRange(names); } /// /// 绑定枚举类型 /// /// /// /// 第一行添加空行 public static void BoundEnum(ComboBoxEdit combo, Type enumType, bool AddEmptyItem) { var names = Enum.GetNames(enumType); combo.Properties.Items.Clear(); combo.Properties.Items.AddRange(names); if (AddEmptyItem) combo.Properties.Items.Insert(0, ""); } public static void BoundBusinessTables(RepositoryItemLookUpEdit lue, bool AddNull) { lue.Columns.Clear(); InitializeControl(lue, new[] { "编号", "单据名称" }, new[] { "DocCode", "FormCaption" }); lue.Columns[0].Width = 40; lue.Columns[1].Width = 100; lue.PopupWidth = 140; var dt = DataDictCache.Cache.BusinessTables.Copy(); if (AddNull) DataTableTools.AddFirstEmptyRow(dt); DataBinder.BindingLookupEditDataSource(lue, dt, "FormCaption", "DocCode"); } public static void BoundBusinessTables(LookUpEdit lue, DataTable dataSource) { lue.Properties.Columns.Clear(); InitializeControl(lue, new[] { "单据名称" }, new[] { "FormName" }); lue.Properties.Columns[0].Width = 140; lue.Properties.PopupWidth = 140; DataBinder.BindingLookupEditDataSource(lue, dataSource, "FormName", "DocCode"); } public static void BoundBusinessTables(LookUpEdit lue, bool AddNull) { lue.Properties.Columns.Clear(); InitializeControl(lue, new[] { "单据字头", "单据名称" }, new[] { "DocCode", "FormCaption" }); lue.Properties.Columns[0].Width = 40; lue.Properties.Columns[1].Width = 100; lue.Properties.PopupWidth = 140; var dt = DataDictCache.Cache.BusinessTables.Copy(); if (AddNull) DataTableTools.AddFirstEmptyRow(dt); DataBinder.BindingLookupEditDataSource(lue, dt, "FormCaption", "DocCode"); } #endregion } }