using DevExpress.XtraEditors.Controls;
|
using System.Collections.Specialized;
|
using DevExpress.XtraGrid.Views.Base;
|
using System.Windows.Forms;
|
using System.Reflection;
|
using System.ComponentModel;
|
using System.IO;
|
using DevExpress.XtraPivotGrid;
|
using DevExpress.XtraPrinting;
|
using DevExpress.XtraGrid;
|
using System.Data;
|
using DevExpress.XtraGrid.Views.Grid;
|
using DevExpress.XtraGrid.Columns;
|
using System.Collections.Generic;
|
using System;
|
using static System.Windows.Forms.Control;
|
|
namespace GSBase.DataA
|
{
|
public enum ExcelFastType
|
{
|
Excel95_2003,
|
Excel2007_More
|
}
|
/// <summary>
|
/// dxGrid相关的辅助方法
|
/// </summary>
|
public sealed class DXGridHelper
|
{
|
private const string ExtendedOfColumnWidth = "ColumnWidth";
|
|
|
|
/// <summary>
|
/// 为表格绑定数据源
|
/// </summary>
|
/// <param name="gridControl">表格控件</param>
|
/// <param name="dcTable">数据表</param>
|
public static void BindDataTable(GridControl gridControl, DataTable dcTable)
|
{
|
BindDataTable(gridControl, dcTable, null);
|
}
|
/// <summary>
|
/// 为表格绑定数据源
|
/// </summary>
|
/// <param name="gridControl">表格控件</param>
|
/// <param name="dcTable">数据表</param>
|
/// <param name="HideList">需要隐藏的字段列表。</param>
|
public static void BindDataTable(GridControl gridControl, DataTable dcTable, StringCollection HideList)
|
{
|
if (gridControl == null || dcTable == null)
|
return;
|
|
gridControl.DataSource = null;
|
//判断如果gridControl还未定义主视图,如果值为空则创建一个默认的视图。
|
if (gridControl.MainView == null)
|
gridControl.MainView = new GridView();
|
|
GridView gv = gridControl.MainView as GridView;
|
|
//清除表格中原有的列
|
gv.Columns.Clear();
|
|
//设置表格的样式
|
gv.OptionsView.ColumnAutoWidth = false;
|
gv.OptionsView.ShowGroupPanel = false;
|
int vi = gv.Columns.Count;
|
//int width;
|
vi++;
|
List<GridColumn> vcolumns = new List<GridColumn>();
|
foreach (DataColumn col in dcTable.Columns)
|
{
|
//从DC中表定义的扩展属性中取出列宽属性.
|
//width = col.MaxLength * 4;
|
|
GridColumn gcol = new GridColumn();
|
gcol.Caption = col.Caption;
|
//gcol.Width = width;
|
gcol.Name = col.ColumnName;
|
gcol.FieldName = col.ColumnName;
|
|
//如果DC中定义有Width并且值大于5,表示显示此列,否则此列不显示。如是不是DC中定义的列,则获取的width值为0
|
if (HideList == null || HideList.IndexOf(col.ColumnName) < 0)
|
{
|
//检测是否为隐藏字段
|
gcol.VisibleIndex = vi++;
|
}
|
|
//TODO:列文本对齐方式未在扩展属性中定义,如果要设置数值类型右对刘,请在此处理
|
vcolumns.Add(gcol);
|
}
|
|
gv.Columns.AddRange(vcolumns.ToArray());
|
gridControl.DataSource = dcTable;
|
//让所有的列调整为合适的列宽。
|
gv.BestFitColumns();
|
}
|
|
/// <summary>
|
/// 获取数据列的默认值宽度。
|
/// </summary>
|
/// <param name="column">数据列。</param>
|
/// <returns></returns>
|
public static int GetExtendedOfColumnWidth(DataColumn column)
|
{
|
string widthObj = GetColumnExtendedProperty(column, "ColumnWidth");
|
return (widthObj != null || widthObj == string.Empty) ? Convert.ToInt32(widthObj) : 100;
|
}
|
|
/// <summary>
|
/// 得到列的扩展属性值
|
/// </summary>
|
/// <param name="column">数据列</param>
|
/// <param name="extendedName">扩展属性名称</param>
|
/// <returns>返回属性值</returns>
|
public static string GetColumnExtendedProperty(DataColumn column, string extendedName)
|
{
|
object val = null;
|
if (column.ExtendedProperties.ContainsKey(extendedName))
|
val = column.ExtendedProperties[extendedName];
|
|
if (val != null)
|
return val.ToString();
|
else
|
return null;
|
}
|
|
|
/// <summary>
|
/// 得到多选表格的数据行
|
/// </summary>
|
public static List<DataRow> getSelectedHeader(GridView View)
|
{
|
int[] selected = View.GetSelectedRows();
|
List<DataRow> result = new List<DataRow>();
|
if (selected == null || selected.Length < 1)
|
{
|
DataRow row = View.GetFocusedDataRow();
|
if (row == null) { return null; }
|
result.Add(row);
|
}
|
|
foreach (int i in selected)
|
{
|
result.Add(View.GetDataRow(i));
|
}
|
return result;
|
}
|
|
/// <summary>
|
/// 把字段绑定到ComboBoxItemCollection
|
/// </summary>
|
/// <param name="Table">数据表</param>
|
/// <param name="FieldName">字段名</param>
|
/// <param name="Items">集合</param>
|
public static void LoadFieldToItems(DataTable Table, string FieldName, ComboBoxItemCollection Items)
|
{
|
if (Table == null || FieldName == null || FieldName == "" || Items == null)
|
return;
|
//把当前字段加入Items
|
Items.Clear();
|
foreach (DataRow Row in Table.Rows)
|
{
|
Items.Add(Row[FieldName].ToString());
|
}
|
}
|
|
/// <summary>
|
/// 冻结网络中的列,从第n个进行冻结
|
/// </summary>
|
/// <param name="gv">网格</param>
|
/// <param name="startCol">冻结的开始列</param>
|
/// <returns></returns>
|
public static bool FixGridColumn(GridView gv, int startCol)
|
{
|
if (startCol <= 0)
|
return false;
|
|
//先取消所有冻结
|
for (int i = 0; i <= gv.Columns.Count - 1; i++)
|
{
|
gv.Columns[i].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.None;
|
}
|
|
//冻结
|
for (int i = 0; i <= startCol; i++)
|
{
|
gv.Columns[i].Fixed = DevExpress.XtraGrid.Columns.FixedStyle.Left;
|
}
|
return true;
|
}
|
|
|
/// <summary>
|
/// 设置表格为只读,并将对应的列设置为非只读
|
/// </summary>
|
/// <param name="View">表格视图</param>
|
/// <param name="colName">列集合名称,用逗号或分号隔开</param>
|
/// <returns></returns>
|
public static bool SetGridColReadOnly(GridView View, string colName)
|
{
|
View.OptionsBehavior.ReadOnly = false;
|
for (int i = 0; i < View.Columns.Count; i++)
|
{
|
View.Columns[i].OptionsColumn.ReadOnly = true;
|
}
|
|
if (!string.IsNullOrEmpty(colName))
|
{
|
string[] cols = colName.Split(new char[2] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
|
foreach (string col in cols)
|
{
|
View.Columns[col].OptionsColumn.ReadOnly = false;
|
}
|
}
|
return true;
|
}
|
|
///// <summary>
|
///// 保存数据时提交表格的修改
|
///// </summary>
|
///// <param name="ControlList">The control list.</param>
|
///// <param name="FormName">窗口名称</param>
|
///// <param name="StyleType">样式类型."Save","Recover"</param>
|
///// <param name="typename">The typename.</param>
|
//public static void OperateStyle(ControlCollection ControlList, string FormName, string StyleType, string typename)
|
//{
|
// GridControl grid = null;
|
// foreach (ListControl control in ControlList)
|
// {
|
// if (control is GridControl)
|
// {
|
// //提交表格的修改
|
// grid = (control as GridControl);
|
// if (grid.MainView is GridView)
|
// {
|
// if (StyleType == "Save")
|
// SaveStyle(grid, FormName, typename);
|
// else
|
// RecoverStyle(grid, FormName, typename);
|
// }
|
// }
|
// else if (control.HasChildren == true)
|
// {
|
// //如果有下层组件,则进行递归调用。
|
// OperateStyle(control.Controls, FormName, StyleType, typename);
|
// }
|
// }
|
//}
|
|
//private const string StylePath = @"C:\\Shintech\\";
|
|
/// <summary>
|
/// 保存样式
|
/// </summary>
|
/// <param name="grid">表格</param>
|
/// <param name="moduleid">模块编号</param>
|
/// <param name="saveType">保存类型</param>
|
private static void SaveStyle(GridControl grid, string moduleid, string saveType)
|
{
|
string path = Application.StartupPath + "\\窗体风格\\" + moduleid + "\\" + grid.Name + "\\";
|
string file = saveType + "view.xml";
|
if (!Directory.Exists(path))
|
{
|
Directory.CreateDirectory(path);
|
}
|
grid.MainView.SaveLayoutToXml(path + file);
|
}
|
|
/// <summary>
|
/// 恢复样式
|
/// </summary>
|
/// <param name="grid">表格</param>
|
/// <param name="moduleid">模块编号</param>
|
/// <param name="saveType">保存类型</param>
|
private static void RecoverStyle(GridControl grid, string moduleid, string saveType)
|
{
|
//由于实装的电脑对Program Files文件夹做了权限控制。风格文件夹无法创建。
|
//string path = StylePath + "窗体风格\\" + moduleid + "\\" + grid.Name + "\\";
|
string path = Application.StartupPath + "\\窗体风格\\" + moduleid + "\\" + grid.Name + "\\";
|
string file = saveType + "view.xml";
|
|
if (!Directory.Exists(path))
|
{
|
Directory.CreateDirectory(path);
|
}
|
if (!File.Exists(path + file))
|
return;
|
grid.MainView.RestoreLayoutFromXml(path + file);
|
}
|
|
/// <summary>
|
/// 隐藏列
|
/// </summary>
|
/// <param name="gv">网格</param>
|
/// <param name="ColNames">列名称集合</param>
|
/// <returns></returns>
|
public static void HideGridColumn(GridView gv, string ColNames)
|
{
|
string[] cols = ColNames.Split(new char[2] { ';', ',' });
|
foreach (string col in cols)
|
{
|
GridColumn gcol = gv.Columns.ColumnByFieldName(col);
|
if (gcol == null)
|
continue;
|
gv.Columns[col].Visible = false;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 列点击开窗事件辅助方法
|
/// </summary>
|
public class ColumnInputHelper
|
{
|
private GridView _view;
|
/// <summary>
|
/// GridView
|
/// </summary>
|
public GridView Gview
|
{
|
get { return _view; }
|
set { _view = value; }
|
}
|
|
private GridColumn _column;
|
/// <summary>
|
/// 数据列
|
/// </summary>
|
public GridColumn Column
|
{
|
get { return _column; }
|
set { _column = value; }
|
}
|
|
private string _dataCatalogID;
|
/// <summary>
|
/// DataCataLog编号
|
/// </summary>
|
public string DataCatalogID
|
{
|
get { return _dataCatalogID; }
|
set { _dataCatalogID = value; }
|
}
|
|
private string _filter;
|
/// <summary>
|
/// 筛选条件
|
/// </summary>
|
public string Filter
|
{
|
get { return _filter; }
|
set { _filter = value; }
|
}
|
|
private DataTable _inputDataTable;
|
/// <summary>
|
/// 传入数据表,若不从DC中读取,也可直接传入数据表
|
/// </summary>
|
public DataTable InputDataTable
|
{
|
get { return _inputDataTable; }
|
set { _inputDataTable = value; }
|
}
|
|
private string _othermapping;
|
/// <summary>
|
/// 其他映射关系(用分号隔开)[Field1=字段1];[Field2=字段2]
|
/// </summary>
|
public string OtherMapping
|
{
|
get { return _othermapping; }
|
set { _othermapping = value; }
|
}
|
|
private string _textField;
|
/// <summary>
|
/// 数据列对应的字段编号
|
/// /// </summary>
|
public string TextField
|
{
|
get { return _textField; }
|
set { _textField = value; }
|
}
|
|
private EntityClass _SelectEntity;
|
/// <summary>
|
/// 查询实体类
|
/// </summary>
|
/// <value>The select entity.</value>
|
//public EntityClass SelectEntity
|
//{
|
// get { return _SelectEntity; }
|
// set { _SelectEntity = value; }
|
|
//}
|
|
private bool _MutilSelect;
|
/// <summary>
|
/// 是否可以多选
|
/// </summary>
|
/// <value>The mutil select.</value>
|
public bool MutilSelect
|
{
|
get { return _MutilSelect; }
|
set { _MutilSelect = value; }
|
|
}
|
|
private string _HideField;
|
/// <summary>
|
/// 隐藏字段,例如单价字段是有权限才使用的。
|
/// </summary>
|
/// <value>需要隐藏的字段列表。如果是多个字段请用分号间隔,例如:DCB009;DCB010。</value>
|
public string HideField
|
{
|
get { return _HideField; }
|
set { _HideField = value; }
|
}
|
|
private bool _DuplicateCheck;
|
/// <summary>
|
/// 是否检测重复记录。
|
/// </summary>
|
/// <value>如果为True表示要检测,否则不检测。</value>
|
public bool DuplicateCheck
|
{
|
get { return _DuplicateCheck; }
|
set { _DuplicateCheck = value; }
|
}
|
|
private bool _IsValidate;
|
/// <summary>
|
/// 是否检测重复记录。
|
/// </summary>
|
/// <value>如果为True表示要检测,否则不检测。</value>
|
public bool IsValidate
|
{
|
get { return _IsValidate; }
|
set { _IsValidate = value; }
|
}
|
|
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
/// <param name="entity">查询实体类,主要用于查询数据库</param>
|
/// <param name="datacatalog">DC编号</param>
|
/// <param name="filter">额外的筛选条件</param>
|
/// <param name="mapping">映射关系,例如从品号带出品名和规格时可以这样写:[DCB005]=MBA002;[DCB006]=MBA003</param>
|
/// <param name="textField">当前列在DC中对应的字段编号。MBA001</param>
|
//public ColumnInputHelper(EntityClass entity, string datacatalog, string filter, string mapping, string textField)
|
//{
|
// InitHelper(entity, datacatalog, filter, mapping, textField, false, "", false, true);
|
//}
|
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
/// <param name="entity">查询实体类,主要用于查询数据库</param>
|
/// <param name="datacatalog">DC编号</param>
|
/// <param name="filter">额外的筛选条件</param>
|
/// <param name="mapping">映射关系,例如从品号带出品名和规格时可以这样写:[DCB005]=MBA002;[DCB006]=MBA003</param>
|
/// <param name="textField">当前列在DC中对应的字段编号。MBA001</param>
|
/// <param name="MutilSelect">是否支持多选。因为有些单据是需要多选的。</param>
|
//public ColumnInputHelper(EntityClass entity, string datacatalog, string filter, string mapping, string textField, bool MutilSelect)
|
//{
|
// InitHelper(entity, datacatalog, filter, mapping, textField, MutilSelect, "", false, true);
|
//}
|
|
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
/// <param name="entity">查询实体类,主要用于查询数据库</param>
|
/// <param name="datacatalog">DC编号</param>
|
/// <param name="filter">额外的筛选条件</param>
|
/// <param name="mapping">映射关系,例如从品号带出品名和规格时可以这样写:[DCB005]=MBA002;[DCB006]=MBA003</param>
|
/// <param name="textField">当前列在DC中对应的字段编号。MBA001</param>
|
/// <param name="MutilSelect">是否支持多选。因为有些单据是需要多选的。</param>
|
/// <param name="IsValidate">此列是否要验证</param>
|
//public ColumnInputHelper(EntityClass entity, string datacatalog, string filter, string mapping, string textField, bool MutilSelect, bool IsValidate)
|
//{
|
// InitHelper(entity, datacatalog, filter, mapping, textField, MutilSelect, "", false, IsValidate);
|
//}
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
/// <param name="entity">查询实体类,主要用于查询数据库</param>
|
/// <param name="datacatalog">DC编号</param>
|
/// <param name="filter">额外的筛选条件</param>
|
/// <param name="mapping">映射关系,例如从品号带出品名和规格时可以这样写:[DCB005]=MBA002;[DCB006]=MBA003</param>
|
/// <param name="textField">当前列在DC中对应的字段编号。MBA001</param>
|
/// <param name="MutilSelect">是否支持多选。因为有些单据是需要多选的。</param>
|
/// <param name="HideField">需要隐藏的字段列表,此功能主要是支持单价权限。如果是多个字段请用分号间隔,例如:DCB009;DCB010。</param>
|
//public ColumnInputHelper(EntityClass entity, string datacatalog, string filter, string mapping,
|
// string textField, bool MutilSelect, string HideField)
|
//{
|
// InitHelper(entity, datacatalog, filter, mapping, textField, MutilSelect, HideField, false, true);
|
//}
|
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
/// <param name="entity">查询实体类,主要用于查询数据库</param>
|
/// <param name="datacatalog">DC编号</param>
|
/// <param name="filter">额外的筛选条件</param>
|
/// <param name="mapping">映射关系,例如从品号带出品名和规格时可以这样写:[DCB005]=MBA002;[DCB006]=MBA003</param>
|
/// <param name="textField">当前列在DC中对应的字段编号。MBA001</param>
|
/// <param name="MutilSelect">是否支持多选。因为有些单据是需要多选的。</param>
|
/// <param name="HideField">需要隐藏的字段列表,此功能主要是支持单价权限。如果是多个字段请用分号间隔,例如:DCB009;DCB010。</param>
|
/// <param name="DuplicateCheck">如果为真,则要检测得的记录。</param>
|
//public ColumnInputHelper(EntityClass entity, string datacatalog, string filter, string mapping,
|
// string textField, bool MutilSelect, string HideField, bool DuplicateCheck)
|
//{
|
// InitHelper(entity, datacatalog, filter, mapping, textField, MutilSelect, HideField, DuplicateCheck, true);
|
//}
|
|
/// <summary>
|
/// 初始化表格列帮助字段。
|
/// </summary>
|
/// <param name="entity">查询实体类,主要用于查询数据库</param>
|
/// <param name="datacatalog">DC编号</param>
|
/// <param name="filter">额外的筛选条件</param>
|
/// <param name="mapping">映射关系,例如从品号带出品名和规格时可以这样写:[DCB005]=MBA002;[DCB006]=MBA003</param>
|
/// <param name="textField">当前列在DC中对应的字段编号。MBA001</param>
|
/// <param name="MutilSelect">是否支持多选。因为有些单据是需要多选的。</param>
|
/// <param name="HideField">需要隐藏的字段列表,此功能主要是支持单价权限。如果是多个字段请用分号间隔,例如:DCB009;DCB010。</param>
|
/// <param name="DuplicateCheck">如果为真,则要检测重复的记录。</param>
|
/// <param name="IsValidate">是否验证</param>
|
private void InitHelper(EntityClass entity, string datacatalog, string filter, string mapping,
|
string textField, bool MutilSelect, string HideField, bool DuplicateCheck, bool IsValidate)
|
{
|
_SelectEntity = entity;
|
_dataCatalogID = datacatalog;
|
_filter = filter;
|
_othermapping = mapping;
|
_textField = textField;
|
_MutilSelect = MutilSelect;
|
_HideField = HideField;
|
_DuplicateCheck = DuplicateCheck;
|
_IsValidate = IsValidate;
|
}
|
|
}
|
|
/// <summary>
|
/// 控件反射辅助方法
|
/// </summary>
|
public class ControlHelper
|
{
|
/// <summary>
|
/// 根据控件名和属性名取值
|
/// </summary>
|
/// <param name="ClassInstance">控件所在实例</param>
|
/// <param name="ControlName">控件名</param>
|
/// <param name="PropertyName">属性名</param>
|
/// <returns>属性值</returns>
|
public static Object GetValueControlProperty(Object ClassInstance, string ControlName, string PropertyName)
|
{
|
Object Result = null;
|
Type myType = ClassInstance.GetType();
|
|
FieldInfo myFieldInfo = myType.GetField(ControlName, BindingFlags.NonPublic | //"|"为或运算,除非两个位均为0,运算结果为0,其他运算结果均为1
|
BindingFlags.Instance);
|
|
//如果为空则返回NULL值
|
if (myFieldInfo == null)
|
return null;
|
string ClassName = myFieldInfo.FieldType.FullName;
|
if (ClassName == "DevExpress.XtraGrid.Views.Grid.GridView" || ClassName == "DevExpress.XtraGrid.Views.BandedGrid.AdvBandedGridView")
|
{
|
object obj = myFieldInfo.GetValue(ClassInstance);
|
try
|
{
|
Type type = obj.GetType();
|
MethodInfo method = type.GetMethod("GetFocusedRowCellValue", new Type[1] { typeof(System.String) });
|
Result = method.Invoke(obj, new object[1] { PropertyName });
|
return Result;
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show(ex.Message);
|
}
|
}
|
else
|
{
|
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(myFieldInfo.FieldType);
|
PropertyDescriptor myProperty = properties.Find(PropertyName, false);
|
if (myProperty == null)
|
return null;
|
|
Object ctr;
|
ctr = myFieldInfo.GetValue(ClassInstance);
|
try
|
{
|
Result = myProperty.GetValue(ctr);
|
return Result;
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show(ex.Message);
|
}
|
}
|
|
return Result;
|
}
|
|
/// <summary>
|
/// 根据控件名和属性名赋值
|
/// </summary>
|
/// <param name="ClassInstance">控件所在实例</param>
|
/// <param name="ControlName">控件名</param>
|
/// <param name="PropertyName">属性名</param>
|
/// <param name="Value">属性值</param>
|
public static void SetValueControlProperty(Object ClassInstance, string ControlName, string PropertyName, Object Value)
|
{
|
Type myType = ClassInstance.GetType();
|
|
FieldInfo myFieldInfo = myType.GetField(ControlName, BindingFlags.NonPublic
|
| BindingFlags.Instance);
|
|
if (myFieldInfo == null)
|
return;
|
|
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(myFieldInfo.FieldType);
|
PropertyDescriptor myProperty = properties.Find(PropertyName, false); //这里设为True就不用区分大小写了
|
|
if (myProperty == null)
|
return;
|
|
Object ctr;
|
ctr = myFieldInfo.GetValue(ClassInstance); //取得控件实例
|
|
try
|
{
|
myProperty.SetValue(ctr, Value.ToString());
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show(ex.Message);
|
}
|
}
|
|
|
}
|
}
|