#region
using System;
using System.Data;
using CSFrameworkV5.Business;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using CSFrameworkV5.Library.CommonClass;
using CSFrameworkV5.Models;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
#endregion
namespace CSFrameworkV5.Library.CommonForms
{
public partial class frmDataFieldsConfig : frmBase
{
private bllDataFieldConfig _BLL;
private DataConfigType _configType;
private GridView _view;
private frmDataFieldsConfig()
{
InitializeComponent();
_BLL = new bllDataFieldConfig();
}
///
/// 应用表格,显示/隐藏栏位,设置只读和可编辑
///
///
///
public static void ApplyConfig(GridView view, DataConfigType configType)
{
//取当前用户的配置
var userConfig =
new bllDataFieldConfig().GetConfig(Loginer.CurrentUser.Account,
configType.ToStringEx());
ApplyConfig(view, configType, userConfig);
}
private static void ApplyConfig(GridView view,
DataConfigType configType, DataTable userConfig)
{
foreach (GridColumn col in view.Columns)
{
col.Visible = IsVisible(userConfig, col.FieldName);
col.OptionsColumn.AllowEdit =
IsEditable(userConfig, col.FieldName);
col.OptionsColumn.ReadOnly =
col.OptionsColumn.AllowEdit == false;
col.VisibleIndex = col.Visible
? GetIndex(userConfig, col.FieldName)
: -1; //只有栏目可见时设置显示序号
}
}
private void btnApply_Click(object sender, EventArgs e)
{
var dt = gc.DataSource as DataTable;
var success = _BLL.Update(dt);
if (success)
{
ApplyConfig(_view, _configType, dt);
Msg.ShowInformation("保存成功!");
Close();
}
}
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
///
/// 打开配置窗体
///
/// 视图名称
/// 配置类型
public static void Execute(GridView view, DataConfigType configType)
{
var form = new frmDataFieldsConfig();
form.Init(view, configType);
form.ShowDialog();
}
private void frmDataFieldsConfig_Load(object sender, EventArgs e)
{
//
}
private static int GetIndex(DataTable userConfig, string fieldName)
{
var rs = userConfig.Select(sys_DataFieldConfig.FieldName + "='" +
fieldName + "'");
if (rs.Length > 0) return userConfig.Rows.IndexOf(rs[0]);
return userConfig.Rows.Count - 1;
}
private void Init(GridView view, DataConfigType configType)
{
_view = view;
_configType = configType;
//绑定用户数据源
DataBinder.BindingLookupEditDataSource(txtConfigUser,
DataDictCache.Cache.User, tb_MyUser.UserName,
tb_MyUser.Account);
txtConfigUser.EditValue = Loginer.CurrentUser.Account;
txtConfigName.Text = configType.ToStringEx();
txtConfigUser.Enabled = false; //如果不按用户配置,禁用掉
}
private static bool IsEditable(DataTable userConfig, string fieldName)
{
var rs = userConfig.Select(sys_DataFieldConfig.FieldName + "='" +
fieldName + "'");
if (rs.Length > 0)
return ConvertEx.ToString(
rs[0][sys_DataFieldConfig.IsEditable]) == "Y";
return true;
}
private static bool IsVisible(DataTable userConfig, string fieldName)
{
var rs = userConfig.Select(sys_DataFieldConfig.FieldName + "='" +
fieldName + "'");
if (rs.Length > 0)
return ConvertEx.ToString(
rs[0][sys_DataFieldConfig.IsDisplay]) == "Y";
return true;
}
///
/// 加载配置
///
private void LoadConfig()
{
var user = txtConfigUser.EditValue.ToStringEx();
//取当前用户的配置
var userConfig = _BLL.GetConfig(_configType.ToStringEx());
var gridData = userConfig.Clone();
gridData.Columns.Add("OrderID", typeof(int));
var i = 1;
//加载表格所有栏位
var dt = _view.GridControl.DataSource as DataTable;
foreach (GridColumn col in _view.Columns)
{
var R = gridData.NewRow();
R["OrderID"] = i;
R[sys_DataFieldConfig.ConfigName] = _configType.ToStringEx();
R[sys_DataFieldConfig.UserName] = user;
R[sys_DataFieldConfig.FieldCaption] = col.Caption;
R[sys_DataFieldConfig.FieldName] = col.FieldName;
R[sys_DataFieldConfig.IsDisplay] = col.Visible ? "Y" : "N";
R[sys_DataFieldConfig.IsEditable] =
col.OptionsColumn.AllowEdit &&
col.OptionsColumn.ReadOnly == false
? "Y"
: "N";
gridData.Rows.Add(R);
i++;
}
//绑定表格数据源
gc.DataSource = gridData;
}
private void txtConfigUser_EditValueChanged(object sender, EventArgs e)
{
LoadConfig();
}
}
///
/// 数据配置类型
///
public enum DataConfigType
{
汇总表_Part1,
汇总表_Part2,
汇总表_Part3,
库存明细表
}
}