#region
|
|
using System;
|
using System.Collections.Generic;
|
using System.Data;
|
using System.Drawing;
|
using System.Windows.Forms;
|
using CSFramework.DB;
|
using CSFrameworkV5.Common;
|
using CSFrameworkV5.Core;
|
using CSFrameworkV5.Language;
|
using CSFrameworkV5.Library.CommonClass;
|
using DevExpress.Utils;
|
using DevExpress.XtraEditors;
|
using DevExpress.XtraEditors.Repository;
|
using DevExpress.XtraGrid;
|
using DevExpress.XtraGrid.Views.Grid;
|
|
#endregion
|
|
namespace CSFrameworkV5.Library
|
{
|
/// <summary>
|
/// 支持数据操作功能的窗体基类.
|
/// </summary>
|
public partial class frmBaseDataForm : frmBaseChild, IDataOperatable
|
{
|
public frmBaseDataForm()
|
{
|
InitializeComponent();
|
}
|
|
/// <summary>
|
/// 是否修改了数据
|
/// </summary>
|
public bool IsDataChanged => IsAddOrEditMode;
|
|
/// <summary>
|
/// 明细编辑页是否隐藏
|
/// </summary>
|
protected bool IsHideEditorPage { get; set; }
|
|
/// <summary>
|
/// 获取Summary表的数据源.
|
/// </summary>
|
protected DataTable SummaryTable
|
{
|
get
|
{
|
if (_SummaryView == null) return null;
|
|
return (DataTable)_SummaryView.DataSource;
|
}
|
}
|
|
public virtual string UpdateTypeName
|
{
|
get
|
{
|
if (UpdateType.Add == _UpdateType) return "(新增模式)";
|
|
if (UpdateType.Modify == _UpdateType) return "(修改模式)";
|
|
return "(查看模式)";
|
}
|
}
|
|
/// <summary>
|
/// 是否允许用户操作数据(未使用)
|
/// </summary>
|
public bool AllowDataOperate
|
{
|
get => _AllowDataOperate;
|
set
|
{
|
_AllowDataOperate = value;
|
SetViewMode();
|
}
|
}
|
|
/// <summary>
|
/// 是否数据发生改变
|
/// </summary>
|
public bool DataChanged => IsAddOrEditMode;
|
|
/// <summary>
|
/// 数据操作按钮组
|
/// </summary>
|
/// <returns></returns>
|
public virtual IButtonInfo[] GetDataOperatableButtons()
|
{
|
var list = new List<IButtonInfo>();
|
|
if (ButtonAuthorized(ButtonAuthority.ADD))
|
list.Add(ToolbarRegister.CreateButton(ButtonNameList.btnAdd,
|
LanLib.Get("新增"), ToolBarGroup.数据操作,
|
Globals.LoadBitmap("32_Add.png"), new Size(57, 28), false,
|
true, DoAdd));
|
|
if (ButtonAuthorized(ButtonAuthority.DELETE))
|
list.Add(ToolbarRegister.CreateButton(ButtonNameList.btnDelete,
|
LanLib.Get("删除"), ToolBarGroup.数据操作,
|
Globals.LoadBitmap("32_Del.png"), new Size(57, 28), false,
|
true, DoDelete));
|
|
if (ButtonAuthorized(ButtonAuthority.EDIT))
|
list.Add(ToolbarRegister.CreateButton(ButtonNameList.btnEdit,
|
LanLib.Get("修改"), ToolBarGroup.数据操作,
|
Globals.LoadBitmap("32_Edit.png"), new Size(57, 28), false,
|
true, DoEdit));
|
|
if (ButtonAuthorized(ButtonAuthority.VIEW))
|
list.Add(ToolbarRegister.CreateButton(ButtonNameList.btnView,
|
LanLib.Get("查看"), ToolBarGroup.数据操作,
|
Globals.LoadBitmap("32_View.png"), new Size(57, 28), true,
|
true, DoViewContent));
|
|
//不需要权限控制
|
list.Add(ToolbarRegister.CreateButton(ButtonNameList.btnSave,
|
LanLib.Get("保存"), ToolBarGroup.数据操作,
|
Globals.LoadBitmap("32_Save.png"), new Size(57, 28), false,
|
true, DoSave));
|
list.Add(ToolbarRegister.CreateButton(ButtonNameList.btnCancel,
|
LanLib.Get("取消"), ToolBarGroup.数据操作,
|
Globals.LoadBitmap("32_Cancel.png"), new Size(57, 28), false,
|
true, DoCancel));
|
|
return list.ToArray();
|
}
|
|
/// <summary>
|
/// 是否新增模式
|
/// </summary>
|
public bool IsAddMode => _UpdateType == UpdateType.Add;
|
|
/// <summary>
|
/// 是否新增/修改模式
|
/// </summary>
|
public bool IsAddOrEditMode => _UpdateType == UpdateType.Add ||
|
_UpdateType == UpdateType.Modify;
|
|
/// <summary>
|
/// 是否修改模式
|
/// </summary>
|
public bool IsEditMode => _UpdateType == UpdateType.Modify;
|
|
/// <summary>
|
/// 是否查看模式
|
/// </summary>
|
public bool IsViewMode => _UpdateType == UpdateType.None;
|
|
/// <summary>
|
/// 数据操作状态
|
/// </summary>
|
public UpdateType UpdateType => _UpdateType;
|
|
/// <summary>
|
/// 打印操作按钮组
|
/// </summary>
|
/// <returns></returns>
|
//public IButtonInfo[] GetPrintableButtons()
|
//{
|
// var list = new List<IButtonInfo>();
|
|
// //打印
|
// if (ButtonAuthorized(ButtonAuthority.PRINT))
|
// {
|
// var btnPrint = ToolbarRegister.CreateButton("btnPrint", LanLib.Get("打印"),
|
// ToolBarGroup.打印功能, Globals.LoadBitmap("32_Print.png"), new Size(57, 28), true, true, DoPrint);
|
// list.Add(btnPrint);
|
// }
|
|
// return list.ToArray();
|
//}
|
|
/// <summary>
|
/// 绑定Summary的导航按钮.
|
/// </summary>
|
protected virtual void BindingSummaryNavigator(
|
ControlNavigator navigator,
|
INavigatableControl navigatableControl)
|
{
|
navigator.NavigatableControl = navigatableControl;
|
navigator.ButtonClick += OnSummaryNavigatorButtonClick;
|
}
|
|
/// <summary>
|
/// 检查按钮的权限
|
/// </summary>
|
/// <param name="authorityValue">权限值</param>
|
/// <returns></returns>
|
public override bool ButtonAuthorized(int authorityValue)
|
{
|
//超级用户拥有所有权限
|
//窗体可用权限=2^n= 1+2+4=7
|
//比如新增功能点是2,那么检查新增按钮的方法是: 2 & 7 = 2,表示有权限。
|
//
|
var isAuth = Loginer.CurrentUser.IsAdmin() ||
|
(authorityValue & FormAuthorities) == authorityValue;
|
return isAuth;
|
}
|
|
/// <summary>
|
/// 按钮状态发生变化
|
/// </summary>
|
protected virtual void ButtonStateChanged(UpdateType currentState)
|
{
|
//
|
}
|
|
/// <summary>
|
/// 删除表格内指定行号的记录
|
/// </summary>
|
/// <param name="rowHandle"></param>
|
protected virtual void DeleteSummaryRow(int rowHandle)
|
{
|
if (rowHandle >= 0)
|
if (_SummaryView.DataSource is DataTable)
|
{
|
var dt = (DataTable)_SummaryView.DataSource;
|
dt.Rows.Remove(_SummaryView.GetDataRow(rowHandle));
|
}
|
}
|
|
/// <summary>
|
/// 自动绑定编辑区域的数据源。规则:txt+字段名,或者chk+字段名,其它类型可以扩展
|
/// </summary>
|
/// <param name="editorPanel">Panel容器</param>
|
/// <param name="dataSource">数据源</param>
|
public void DoBindingEditorPanel(Control editorPanel,
|
DataTable dataSource, string ChilddataName)
|
{
|
//代码已分离到公共静态类
|
DataBinder.BindingEditorPanel(editorPanel, dataSource,
|
ChilddataName);
|
//BindingEditorPanel(editorPanel, dataSource, ChilddataName);
|
}
|
|
//private void BindingEditorPanel(Control editorPanel, DataTable dataSource, string childdataName)
|
//{
|
// string fieldName = "";
|
// try
|
// {
|
// for (int i = 0; i <= editorPanel.Controls.Count - 1; i++)
|
// {
|
// if (editorPanel.Controls[i] is BaseEdit)
|
// {
|
// BaseEdit edit = editorPanel.Controls[i] as BaseEdit;
|
// if (edit.Name.Substring(0, 3) == "txt")
|
// {
|
// fieldName = edit.Name.Substring(3, edit.Name.Length - 3);
|
// DataBinder.BindingTextEditBase(edit, dataSource, fieldName);
|
// }
|
// }
|
// if (editorPanel.Controls[i] is GridControl)
|
// {
|
// DataTable Childdata = null;
|
// string CdataColumnsName = childdataName.Substring(childdataName.Length - 3) + "001";//子表主建
|
// GridControl edit = editorPanel.Controls[i] as GridControl;
|
// foreach (DataRow dr in dataSource.Rows)
|
// {
|
// for (int j = 0; j < dataSource.Columns.Count; j++)
|
// {
|
// string str = dataSource.Columns[j].ColumnName.ToString();
|
// if (str.Substring(str.Length - 3) == "001")
|
// {
|
// string con = " Data Source=.;Initial Catalog=CSFrameworkV5_Normal;User ID=sa;Password =563593659liu;Persist Security Info=True;Connect Timeout=15;;Connection TimeOut=15;";
|
// mSSQL = new DatabaseMSSQL(con);
|
// string sql = string.Format(@" SELECT * FROM {0} WHERE {1}='{2}' ",
|
// childdataName, CdataColumnsName, dr[j]);
|
// Childdata = mSSQL.GetTable(sql, childdataName);
|
// }
|
// }
|
// }
|
// edit.DataSource = Childdata;
|
|
// }
|
|
// if (editorPanel.Controls[i] is AdvButtonText)
|
// {
|
// AdvButtonText ButtonText = editorPanel.Controls[i] as AdvButtonText;
|
|
// ButtonText.lbCaption.Text = ButtonText.CaptionT;//"到货单号";
|
// ButtonText.ButtonClick += ButtonText_ButtonClick;
|
// //ButtonText.textEdit.TextChanged -= new EventHandler(textEdit_TextChanged);
|
// //ButtonText.textEdit.TextChanged += new EventHandler(textEdit_TextChanged);
|
// }
|
|
// if (editorPanel.Controls[i] is CheckEdit)
|
// {
|
// CheckEdit edit = editorPanel.Controls[i] as CheckEdit;
|
// if (edit.Name.Substring(0, 3) == "chk")
|
// {
|
// fieldName = edit.Name.Substring(3, edit.Name.Length - 3);
|
// DataBinder.BindingCheckEdit(edit, dataSource, fieldName);
|
// }
|
// }
|
// }
|
// }
|
// catch (Exception ex)
|
// {
|
// LogUserOperate.Write(ex);
|
// Msg.Warning("字段:" + fieldName + "\r\n" + ex.Message);
|
// }
|
//}
|
|
//private void ButtonText_ButtonClick(object sender, EventArgs e)
|
//{
|
// AdvButtonText ButtonText = sender as AdvButtonText;
|
// DataRow datarow = null;
|
// if (_UpdateType == UpdateType.None)
|
// {
|
// Msg.Warning("请先打开新增或修改状态!");
|
// return;
|
// }
|
// try
|
// {
|
// SelectQueryDlg SelectItemDlg = new SelectQueryDlg(ButtonText.DatacatalogID, ButtonText.Filter);
|
// if (SelectItemDlg.ShowDialog() == DialogResult.OK)
|
// datarow = (SelectItemDlg.ReturnRow == null) ? null : SelectItemDlg.ReturnRow;
|
// else
|
// datarow = null;
|
// ButtonText.textEdit.EditValue = datarow[ButtonText.TextField];
|
// return;
|
// }
|
// catch (Exception ex)
|
// {
|
|
// return;
|
// }
|
|
// ////若行为空则返回.
|
// //if (datarow == null)
|
// //{
|
// // return ;
|
// //}
|
// ////对查询出来结果进行赋值
|
|
// ////ControlHelper.SetValueControlProperty(this, ButtonText.Name, "EditValue", datarow[ButtonText.TextField].ToString());
|
|
// ////对备注字段进行赋值,若备注字段为空、文本模式为BeRemark,且数据表中存在字段才执行
|
// //if (ButtonText.TextType == ShowStyle.BeRemark && ButtonText.RemarkField.Length > 0 &&
|
// // datarow.Table.Columns.Contains(ButtonText.RemarkField))
|
// //{
|
// // ButtonText.lbRemark.Text = datarow[ButtonText.RemarkField].ToString();
|
// // //ControlHelper.SetValueControlProperty(this, ButtonText.Name, "Remark", datarow[ButtonText.RemarkField].ToString());
|
// //}
|
// //#region 点选结果返回变更
|
// //ButtonText.Tag = datarow;
|
// ////ButtonText.FireButtonDataChanged(sender, e);
|
// //#endregion
|
|
//}
|
|
/// <summary>
|
/// 自动绑定编辑区域的数据源。规则:txt+字段名,或者chk+字段名,其它类型可以扩展
|
/// </summary>
|
/// <param name="editorPanel">Panel容器</param>
|
/// <param name="dataSource">数据源</param>
|
public void DoBindingEditorPanel(Control editorPanel,
|
DataTable dataSource)
|
{
|
//代码已分离到公共静态类
|
DataBinder.BindingEditorPanel(editorPanel, dataSource);
|
}
|
|
/// <summary>
|
/// 给表格绑定数据
|
/// </summary>
|
/// <param name="dataSource"></param>
|
protected void DoBindingSummaryGrid(DataTable dataSource)
|
{
|
if (_SummaryView != null)
|
{
|
_SummaryView.DataSource = null;
|
_SummaryView.DataSource = dataSource;
|
}
|
}
|
|
/// <summary>
|
/// 第一个编辑控件设置焦点.
|
/// </summary>
|
protected void FocusEditor()
|
{
|
if (_ActiveEditor != null)
|
{
|
if (_ActiveEditor.CanFocus)
|
_ActiveEditor.Focus();
|
else
|
SendKeys.Send("{Tab}"); //发送Tab键
|
}
|
}
|
|
/// <summary>
|
/// 关窗体中...
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void frmBaseDataForm_FormClosing(object sender,
|
FormClosingEventArgs e)
|
{
|
if (DataChanged) e.Cancel = !Msg.AskQuestion("您修改了数据没有保存,确定要退出吗?");
|
}
|
|
private void frmBaseDataForm_Load(object sender, EventArgs e)
|
{
|
gcNavigator.Visible = false;
|
}
|
|
/// <summary>
|
/// 获取指定的资料行
|
/// </summary>
|
protected DataRow GetDataRow(int rowIndex)
|
{
|
if (rowIndex < 0) return null;
|
|
if (SummaryTable != null) return SummaryTable.Rows[rowIndex];
|
|
return null;
|
}
|
|
/// <summary>
|
/// 获取当前光标所在的资料行.
|
/// </summary>
|
protected DataRow GetFocusedRow()
|
{
|
if (_SummaryView.FocusedRowHandle < 0) return null;
|
|
if (_SummaryView.DataSource is DataTable)
|
return _SummaryView.GetDataRow(_SummaryView.FocusedRowHandle);
|
|
return null;
|
}
|
|
public virtual IButtonInfo[] GetNavigatorButtonButtons()
|
{
|
var list = new List<IButtonInfo>();
|
|
list.Add(ToolbarRegister.CreateButton(ButtonNameList.btnMoveFirst,
|
LanLib.Get("最前"), ToolBarGroup.导航功能,
|
Globals.LoadBitmap("16_First.png"), new Size(57, 28), true,
|
false, DoMoveFirst));
|
list.Add(ToolbarRegister.CreateButton(ButtonNameList.btnMoveLast,
|
LanLib.Get("最后"), ToolBarGroup.导航功能,
|
Globals.LoadBitmap("16_Last.png"), new Size(57, 28), false,
|
false, DoMoveLast));
|
list.Add(ToolbarRegister.CreateButton(ButtonNameList.btnMovePrior,
|
LanLib.Get("上条"), ToolBarGroup.导航功能1,
|
Globals.LoadBitmap("16_Prior.png"), new Size(57, 28), false,
|
false, DoMovePrior));
|
list.Add(ToolbarRegister.CreateButton(ButtonNameList.btnMoveNext,
|
LanLib.Get("下条"), ToolBarGroup.导航功能1,
|
Globals.LoadBitmap("16_Next.png"), new Size(57, 28), false,
|
false, DoMoveNext));
|
|
return list.ToArray();
|
}
|
|
/// <summary>
|
/// 表格是否有数据
|
/// </summary>
|
/// <returns></returns>
|
public virtual bool HasData()
|
{
|
if (_SummaryView != null) return _SummaryView.RowCount > 0;
|
|
return false;
|
}
|
|
/// <summary>
|
/// 隐藏明细编辑页【数据编辑】页
|
/// </summary>
|
/// <param name="hideTabHeader">隐藏页面标签按钮</param>
|
protected void HideEditorPage(bool hideTabHeader = true)
|
{
|
if (hideTabHeader)
|
{
|
tcBusiness.ShowTabHeader = DefaultBoolean.False; //隐藏页面标签按钮
|
tcBusiness.SelectedTabPage = tpSummary; //仅显示【数据查询】页
|
}
|
else
|
{
|
tpDetail.PageVisible = false; //隐藏【数据编辑】页
|
}
|
|
IsHideEditorPage = true;
|
}
|
|
/// <summary>
|
/// 初始化数据窗体的按钮
|
/// </summary>
|
public override void InitButtons()
|
{
|
base.InitButtons();
|
|
var navigatorButton = GetNavigatorButtonButtons(); //4个导航按钮
|
var dataButton = GetDataOperatableButtons(); //数据操作按钮
|
//var printButton = GetPrintableButtons(); //打印按钮
|
|
Buttons.AddRange(navigatorButton);
|
Buttons.AddRange(dataButton);
|
// Buttons.AddRange(printButton);
|
}
|
|
/// <summary>
|
/// 自定义初始化窗体操作, 窗体的Load事件必须调用此方法
|
/// </summary>
|
protected virtual void InitializeForm() //此方法由基类的Load事件调用
|
{
|
InitButtons(); //初始化本窗体的按钮
|
SetViewMode(); //预设为数据查看模式
|
|
//无操作状态下不可输入数据
|
SetDetailEditorsAccessable(_DetailGroupControl, false);
|
}
|
|
/// <summary>
|
/// 双击表格事件
|
/// </summary>
|
protected virtual void OnGridViewDoubleClick(object sender, EventArgs e)
|
{
|
try
|
{
|
if (IsHideEditorPage) return;
|
|
if (!HasData()) return;
|
|
var btn = _buttons.GetButtonByName("btnEdit");
|
|
//双击表格进入修改状态
|
if (SystemSettings.Current.DoubleClickEditMode)
|
{
|
if (ButtonAuthorized(ButtonAuthority.EDIT)) //当前用户有修改权限
|
DoEdit(btn); //调用修改方法
|
}
|
else //进入查看状态
|
{
|
DoViewContent(btn);
|
SetDetailEditorsAccessable(_DetailGroupControl, false);
|
}
|
}
|
catch
|
{
|
}
|
}
|
|
/// <summary>
|
/// 主表格导航按钮的事件
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void OnSummaryNavigatorButtonClick(object sender,
|
NavigatorButtonClickEventArgs e)
|
{
|
try
|
{
|
CCursor.ShowWaitCursor();
|
var btn = (NavigatorButton)e.Button;
|
var buttons = ((ControlNavigator)sender).Buttons;
|
if (e.Button == buttons.First) DoMoveFirst(null);
|
|
if (e.Button == buttons.Prev) DoMovePrior(null);
|
|
if (e.Button == buttons.Next) DoMoveNext(null);
|
|
if (e.Button == buttons.Last) DoMoveLast(null);
|
}
|
finally
|
{
|
e.Handled = true;
|
CCursor.ShowDefaultCursor();
|
}
|
}
|
|
/// <summary>
|
/// 替换记录对应字段的数据。
|
/// </summary>
|
/// <param name="sourceRow">数据源</param>
|
/// <param name="destRow">需要替换的记录</param>
|
protected void ReplaceDataRowChanges(DataRow sourceRow, DataRow destRow)
|
{
|
string fieldName;
|
|
//循环处理当前记录的所有字段
|
for (var i = 0; i <= sourceRow.Table.Columns.Count - 1; i++)
|
{
|
fieldName = sourceRow.Table.Columns[i].ColumnName;
|
|
//如果字段名相同,替换对应字段的数据。
|
if (destRow.Table.Columns.IndexOf(fieldName) >= 0)
|
destRow[fieldName] = sourceRow[fieldName];
|
}
|
}
|
|
/// <summary>
|
/// 设置为编辑模式
|
/// 数据操作两种状态.1:数据修改状态 2:查看数据状态
|
/// </summary>
|
protected virtual void SetEditMode()
|
{
|
_buttons.GetButtonByName(ButtonNameList.btnFilter).Enable = false;
|
_buttons.GetButtonByName(ButtonNameList.btnPrint).Enable = false;
|
_buttons.GetButtonByName(ButtonNameList.btnPreview).Enable = false;
|
|
_buttons.GetButtonByName(ButtonNameList.btnAdd).Enable = false;
|
_buttons.GetButtonByName(ButtonNameList.btnDelete).Enable = false;
|
_buttons.GetButtonByName(ButtonNameList.btnEdit).Enable = false;
|
_buttons.GetButtonByName(ButtonNameList.btnView).Enable = false;
|
_buttons.GetButtonByName(ButtonNameList.btnSave).Enable = true;
|
_buttons.GetButtonByName(ButtonNameList.btnCancel).Enable = true;
|
|
_buttons.GetButtonByName(ButtonNameList.btnMoveFirst).Enable =
|
false;
|
_buttons.GetButtonByName(ButtonNameList.btnMovePrior).Enable =
|
false;
|
_buttons.GetButtonByName(ButtonNameList.btnMoveNext).Enable = false;
|
_buttons.GetButtonByName(ButtonNameList.btnMoveLast).Enable = false;
|
}
|
|
/// <summary>
|
/// 给绑定数据源的输入控件赋值
|
/// </summary>
|
protected void SetEditorBindingValue(Control bindingControl,
|
object value)
|
{
|
SetEditorBindingValue(bindingControl, value, true);
|
}
|
|
/// <summary>
|
/// 给绑定数据源的输入控件赋值
|
/// </summary>
|
/// <param name="bindingControl">绑定数据源的组件</param>
|
/// <param name="value">值</param>
|
/// <param name="setEditorValue">给控件赋值</param>
|
protected void SetEditorBindingValue(Control bindingControl,
|
object value, bool setEditorValue)
|
{
|
object temp = null; //空值为null
|
if (value != DBNull.Value) temp = value;
|
|
//有绑定数据源, 给数据源赋值
|
if (bindingControl.DataBindings.Count > 0)
|
{
|
var dataSource = bindingControl.DataBindings[0].DataSource;
|
var field = bindingControl.DataBindings[0].BindingMemberInfo
|
.BindingField;
|
if (dataSource is DataTable)
|
(dataSource as DataTable).Rows[0][field] = value;
|
else
|
DataConverter.SetValueOfObject(dataSource, field, value);
|
}
|
|
//给输入组件的赋值
|
if (setEditorValue)
|
{
|
if (bindingControl is BaseEdit)
|
(bindingControl as BaseEdit).EditValue = value;
|
else
|
DataConverter.SetValueOfObject(bindingControl, "EditValue",
|
value);
|
}
|
}
|
|
/// <summary>
|
/// 设置输入组件只读及背景色
|
/// </summary>
|
/// <param name="editor">输入组件</param>
|
/// <param name="enable">可写/只读</param>
|
/// <param name="setBackgroundColor">是否设置背景色</param>
|
protected void SetEditorEnable(TextEdit editor, bool enable,
|
bool setBackgroundColor)
|
{
|
if (enable && setBackgroundColor) editor.BackColor = Color.White;
|
|
if (!enable && setBackgroundColor)
|
editor.BackColor = SystemColors.ButtonFace;
|
|
editor.Properties.ReadOnly = !enable;
|
}
|
|
/// <summary>
|
/// 设置Grid自定义按钮(Add,Insert,Delete)状态
|
/// </summary>
|
protected void SetGridCustomButtonAccessable(GridControl gridControl,
|
bool value)
|
{
|
var custom = gridControl.EmbeddedNavigator.Buttons.CustomButtons;
|
foreach (NavigatorCustomButton btn in custom) btn.Enabled = value;
|
}
|
|
public override void SetLanguage()
|
{
|
base.SetLanguage();
|
Buttons.GetButtonByName("btnPrint").Caption = LanLib.Get("打印");
|
Buttons.GetButtonByName("btnPreview").Caption = LanLib.Get("预览");
|
Buttons.GetButtonByName("btnView").Caption = LanLib.Get("查看");
|
Buttons.GetButtonByName("btnAdd").Caption = LanLib.Get("新增");
|
Buttons.GetButtonByName("btnDelete").Caption = LanLib.Get("删除");
|
Buttons.GetButtonByName("btnEdit").Caption = LanLib.Get("修改");
|
Buttons.GetButtonByName("btnSave").Caption = LanLib.Get("保存");
|
Buttons.GetButtonByName("btnCancel").Caption = LanLib.Get("取消");
|
|
tpSummary.Text = LanLib.Get("数据查询");
|
tpDetail.Text = LanLib.Get("数据编辑");
|
}
|
|
/// <summary>
|
/// 设置为查看模式
|
/// 数据操作两种状态.1:数据修改状态 2:查看数据状态
|
/// </summary>
|
protected virtual void SetViewMode()
|
{
|
_buttons.GetButtonByName(ButtonNameList.btnFilter).Enable =
|
ButtonAuthorized(ButtonAuthority.VIEW);
|
_buttons.GetButtonByName(ButtonNameList.btnPrint).Enable =
|
ButtonAuthorized(ButtonAuthority.PRINT);
|
_buttons.GetButtonByName(ButtonNameList.btnPreview).Enable =
|
ButtonAuthorized(ButtonAuthority.PREVIEW);
|
|
_buttons.GetButtonByName(ButtonNameList.btnAdd).Enable =
|
ButtonAuthorized(ButtonAuthority.ADD);
|
_buttons.GetButtonByName(ButtonNameList.btnDelete).Enable =
|
ButtonAuthorized(ButtonAuthority.DELETE);
|
_buttons.GetButtonByName(ButtonNameList.btnEdit).Enable =
|
ButtonAuthorized(ButtonAuthority.EDIT);
|
_buttons.GetButtonByName(ButtonNameList.btnView).Enable =
|
ButtonAuthorized(ButtonAuthority.VIEW);
|
_buttons.GetButtonByName(ButtonNameList.btnSave).Enable = false;
|
_buttons.GetButtonByName(ButtonNameList.btnCancel).Enable = false;
|
|
_buttons.GetButtonByName(ButtonNameList.btnMoveFirst).Enable = true;
|
_buttons.GetButtonByName(ButtonNameList.btnMovePrior).Enable = true;
|
_buttons.GetButtonByName(ButtonNameList.btnMoveNext).Enable = true;
|
_buttons.GetButtonByName(ButtonNameList.btnMoveLast).Enable = true;
|
}
|
|
/// <summary>
|
/// 显示明细页
|
/// </summary>
|
protected virtual void ShowDetailPage(bool disableSummaryPage)
|
{
|
try
|
{
|
SuspendLayout();
|
tpDetail.PageEnabled = true;
|
tcBusiness.SelectedTabPage = tpDetail;
|
tpSummary.PageEnabled = !disableSummaryPage;
|
FocusEditor(); //第一个编辑框获得焦点.
|
ResumeLayout();
|
}
|
catch (Exception ex)
|
{
|
ResumeLayout();
|
Msg.ShowException(ex);
|
}
|
}
|
|
/// <summary>
|
/// 显示主表页
|
/// </summary>
|
protected virtual void ShowSummaryPage(bool disableDetailPage)
|
{
|
try
|
{
|
SuspendLayout();
|
tpSummary.PageEnabled = true;
|
tcBusiness.SelectedTabPage = tpSummary;
|
tpDetail.PageEnabled = !disableDetailPage;
|
if (_SummaryView != null) _SummaryView.SetFocus();
|
|
ResumeLayout();
|
}
|
catch (Exception ex)
|
{
|
ResumeLayout();
|
Msg.ShowException(ex);
|
}
|
}
|
|
/// <summary>
|
/// 显示当前操作消息
|
/// </summary>
|
protected void ShowTip(string tip)
|
{
|
lblPrompt.Text = tip;
|
lblPrompt.Update();
|
}
|
|
/// <summary>
|
/// 在保存时有此情况发生: 不会更新最后一个编辑框里的数据!
|
/// 当移除焦点后会更新输入框的数据.
|
/// </summary>
|
protected void UpdateLastControl()
|
{
|
try
|
{
|
if (ActiveControl == null) return;
|
|
var ctl = ActiveControl;
|
txtFocusForSave.Focus();
|
ActiveControl = ctl;
|
}
|
catch (Exception ex)
|
{
|
Msg.ShowException(ex);
|
}
|
}
|
|
/// <summary>
|
/// 更新当前操作的缓存记录
|
/// 保存数据后更新Summary当前记录.
|
/// 如果是修改后保存,将最新数据替换当前记录的数据.
|
/// 如果是新增后保存,在表格内插入一条记录.
|
/// </summary>
|
/// <param name="summary">查询表格中的当前记录</param>
|
protected virtual void UpdateSummaryRow(DataRow summary)
|
{
|
UpdateSummaryRow(summary, _UpdateType);
|
}
|
|
/// <summary>
|
/// 更新当前操作的缓存记录(主表表格中当前记录)
|
/// </summary>
|
/// <param name="summary">查询表格中的当前记录</param>
|
/// <param name="updateType">操作状态</param>
|
protected virtual void UpdateSummaryRow(DataRow summary,
|
UpdateType updateType)
|
{
|
try
|
{
|
if (summary == null) return;
|
|
//容错性:若主表表格未初始化数据源,取当前修改的数据作为数据源
|
if (_SummaryView.DataSource == null)
|
_SummaryView.DataSource = summary.Table.Clone();
|
|
var dt = (DataTable)_SummaryView.DataSource;
|
|
//如果是新增后保存,在表格内插入一条记录.
|
if (updateType == UpdateType.Add)
|
{
|
var newrow = dt.NewRow(); //表格的数据源增加一条记录
|
DataConverter.ReplaceDataRowChanges(summary, newrow); //替换数据
|
dt.Rows.Add(newrow);
|
_SummaryView.RefreshDataSource(); //刷新数据源
|
|
//取记录的序号
|
if (_SummaryView.View is GridView)
|
{
|
//定位当前记录
|
var i = (_SummaryView.View as GridView).GetRowHandle(
|
dt.Rows.IndexOf(newrow));
|
_SummaryView.FocusedRowHandle = i;
|
}
|
else //定位最后一条记录
|
{
|
_SummaryView.FocusedRowHandle = dt.Rows.Count - 1;
|
}
|
|
dt.AcceptChanges();
|
}
|
|
//如果是修改后保存,将最新数据替换当前记录的数据.
|
if (updateType == UpdateType.Modify ||
|
updateType == UpdateType.None)
|
{
|
var focusedRowHandle = _SummaryView.FocusedRowHandle;
|
var dr = _SummaryView.GetDataRow(focusedRowHandle);
|
DataConverter.ReplaceDataRowChanges(summary, dr); //替换数据
|
dr.Table.AcceptChanges();
|
_SummaryView.RefreshRow(
|
focusedRowHandle); //修改或新增要刷新Grid数据
|
}
|
}
|
catch (Exception ex)
|
{
|
LogUserOperate.Write(ex);
|
Msg.ShowException(ex);
|
}
|
}
|
|
#region IPrintableForm 成员
|
|
/// <summary>
|
/// 打印报表
|
/// </summary>
|
/// <param name="button"></param>
|
//public virtual void DoPrint(IButtonInfo button)
|
//{
|
// DataSet ds = DAL.GetMasterDetailData();
|
// Msg.ShowInformation("没有实现打印功能!");
|
|
|
//}
|
|
#endregion
|
|
#region 成员变量
|
|
/// <summary>
|
/// 当显示修改明细页时,首先获取焦点的编辑框.
|
/// </summary>
|
protected Control _ActiveEditor;
|
|
public static DatabaseMSSQL mSSQL = null;
|
public static DatabaseMSSQL mSSQLSYS = null;
|
|
/// <summary>
|
/// 关键字段输入框,新增时不可修改
|
/// </summary>
|
protected Control _KeyEditor;
|
|
/// <summary>
|
/// 主表的数据表格对象,必须由派生类指定表格类型。
|
/// 因Dev GridControl组件不可继承所以子类窗体Load的时候需要赋值.
|
/// </summary>
|
protected ISummaryView _SummaryView;
|
|
/// <summary>
|
/// 数据编辑页的主容器
|
/// 因继承问题,需要在子类窗体Load的时候需要赋值
|
/// </summary>
|
protected Control _DetailGroupControl;
|
|
/// <summary>
|
/// 数据操作状态
|
/// </summary>
|
protected UpdateType _UpdateType = UpdateType.None;
|
|
protected virtual string GetStateName()
|
{
|
if (UpdateType.Add == _UpdateType) return "(新增模式)";
|
|
if (UpdateType.Modify == _UpdateType) return "(修改模式)";
|
|
return "(查看模式)";
|
}
|
|
/// <summary>
|
/// 是否允许用户操作数据
|
/// </summary>
|
protected bool _AllowDataOperate = true;
|
|
#endregion
|
|
#region IDataOperatable接口的方法
|
|
/// <summary>
|
/// 高级查询模板方法(虚方法)
|
/// </summary>
|
/// <param name="sender"></param>
|
public virtual void DoFilter(IButtonInfo sender)
|
{
|
Msg.ShowInformation("未实现此功能,参考frmPO,frmSO实例!");
|
}
|
|
/// <summary>
|
/// 查看选中记录的数据
|
/// </summary>
|
/// <param name="sender"></param>
|
public virtual void DoViewContent(IButtonInfo sender)
|
{
|
ButtonStateChanged(_UpdateType);
|
}
|
|
/// <summary>
|
/// 新增记录
|
/// </summary>
|
/// <param name="sender"></param>
|
public virtual void DoAdd(IButtonInfo sender)
|
{
|
_UpdateType = UpdateType.Add;
|
SetEditMode();
|
ButtonStateChanged(_UpdateType);
|
}
|
|
/// <summary>
|
/// 修改数据
|
/// </summary>
|
/// <param name="sender"></param>
|
public virtual void DoEdit(IButtonInfo sender)
|
{
|
_UpdateType = UpdateType.Modify;
|
SetEditMode();
|
ButtonStateChanged(_UpdateType);
|
}
|
|
/// <summary>
|
/// 取消新增或修改
|
/// </summary>
|
/// <param name="sender"></param>
|
public virtual void DoCancel(IButtonInfo sender)
|
{
|
try
|
{
|
frmWaitingEx.ShowMe(this);
|
|
_UpdateType = UpdateType.None;
|
SetViewMode();
|
ButtonStateChanged(_UpdateType);
|
|
if (_UpdateType == UpdateType.Add)
|
ShowSummaryPage(true);
|
else if (_SummaryView.RowCount > 0)
|
DoViewContent(sender);
|
else
|
ShowSummaryPage(true);
|
}
|
catch (Exception ex)
|
{
|
LogUserOperate.Write(ex);
|
Msg.ShowException(ex);
|
}
|
finally
|
{
|
frmWaitingEx.HideMe(this);
|
}
|
}
|
|
/// <summary>
|
/// 保存数据
|
/// </summary>
|
/// <param name="sender"></param>
|
public virtual void DoSave(IButtonInfo sender)
|
{
|
DoSave_ResetState();
|
}
|
|
/// <summary>
|
/// 数据窗体保存数据后重置操作状态
|
/// </summary>
|
protected virtual void DoSave_ResetState()
|
{
|
//适用性:当派生窗体重载DoSave方法,提交数据成功后调用此方法而不是调用base.DoSave()
|
_UpdateType = UpdateType.None;
|
SetViewMode();
|
ShowDetailPage(false);
|
ButtonStateChanged(_UpdateType);
|
}
|
|
/// <summary>
|
/// 删除记录
|
/// </summary>
|
/// <param name="sender"></param>
|
public virtual void DoDelete(IButtonInfo sender)
|
{
|
}
|
|
#endregion
|
|
#region Set Editors Accessable
|
|
/// <summary>
|
/// 控制[明细编辑页面]上的所有可输入控件是否可编辑.
|
/// </summary>
|
protected virtual void SetDetailEditorsAccessable(Control panel,
|
bool value)
|
{
|
if (panel == null) return;
|
|
controlNavigatorSummary.Enabled = !value;
|
SetControlAccessableCycle(panel, value);
|
}
|
|
/// <summary>
|
/// 设置容器内的控件可用状态, Control.Enable = false/true
|
/// </summary>
|
/// <param name="container">容器</param>
|
/// <param name="value">false/true</param>
|
public static void SetControlEnable(Control container, bool value)
|
{
|
if (container is Label) return;
|
|
if (container is LabelControl) return;
|
|
if (container.Name == "") return;
|
|
if (container.Controls.Count > 0)
|
foreach (Control c in container.Controls)
|
{
|
c.Enabled = value;
|
SetControlEnable(c, value);
|
}
|
}
|
|
/// <summary>
|
/// 设置容器内所有可输入控件的状态 .ReadOnly or Enable . (递归)循环控制
|
/// </summary>
|
/// <param name="container">容器</param>
|
/// <param name="value">false/true</param>
|
protected void SetControlAccessableCycle(Control container, bool value)
|
{
|
if (container.HasChildren)
|
foreach (Control ctrl in container.Controls)
|
//DevExpress的内部(Inner)控件
|
if (ctrl.Name == string.Empty)
|
SetControlAccessable(container, value);
|
else
|
SetControlAccessableCycle(ctrl, value);
|
else
|
SetControlAccessable(container, value);
|
}
|
|
/// <summary>
|
/// 设置容器内所有可输入控件的状态.ReadOnly or Enable = false/true
|
/// </summary>
|
/// <param name="container">容器</param>
|
/// <param name="value">false/true</param>
|
protected void SetControlAccessable(Control container, bool value)
|
{
|
if (container is Label) return;
|
|
if (container is LabelControl) return;
|
|
if (container is UserControl) return;
|
|
if (container.HasChildren == false) return; //没有子控件,不处理
|
|
foreach (Control c in container.Controls)
|
//最常用组件,首先处理
|
if (c is BaseEdit) //输入框
|
(c as BaseEdit).Properties.ReadOnly = !value;
|
else if (c is GridControl) //表格
|
((c as GridControl).Views[0] as GridView).OptionsBehavior
|
.Editable = value;
|
else
|
SetControlAccessableByProp(container,
|
value); //其它组件,反射属性设置状态
|
}
|
|
/// <summary>
|
/// 设置一个控件的可用状态,通过反射ReadOnly,Properties属性
|
/// </summary>
|
/// <param name="control">控件</param>
|
/// <param name="value">值</param>
|
private void SetControlAccessableByProp(Control control, bool value)
|
{
|
var type = control.GetType();
|
var infos = type.GetProperties();
|
foreach (var info in infos)
|
{
|
if (info.Name == "ReadOnly") //Properties.ReadOnly
|
{
|
info.SetValue(control, !value, null);
|
return;
|
}
|
|
if (info.Name == "Properties")
|
{
|
var o = info.GetValue(control, null);
|
|
//处理特殊组件
|
if (o is RepositoryItemButtonEdit &&
|
(o as RepositoryItemButtonEdit).Buttons.Count >
|
0) //ButtonEdit
|
(o as RepositoryItemButtonEdit).Buttons[0].Enabled =
|
value;
|
else if (o is RepositoryItemDateEdit &&
|
(o as RepositoryItemDateEdit).Buttons.Count >
|
0) //DateEdit
|
(o as RepositoryItemDateEdit).Buttons[0].Enabled =
|
value;
|
|
if (o is RepositoryItem)
|
(o as RepositoryItem).ReadOnly = !value;
|
|
return;
|
}
|
}
|
}
|
|
#endregion
|
|
#region Summary数据导航功能
|
|
/// <summary>
|
/// 移到第一条记录
|
/// </summary>
|
protected virtual void DoMoveFirst(IButtonInfo sender)
|
{
|
if (_SummaryView == null) return;
|
|
_SummaryView.MoveFirst();
|
if (tcBusiness.SelectedTabPage != tpSummary) DoViewContent(null);
|
}
|
|
/// <summary>
|
/// 移到上一条记录
|
/// </summary>
|
protected virtual void DoMovePrior(IButtonInfo sender)
|
{
|
if (_SummaryView == null) return;
|
|
_SummaryView.MovePrior();
|
if (tcBusiness.SelectedTabPage != tpSummary) DoViewContent(null);
|
}
|
|
/// <summary>
|
/// 移到下一条记录
|
/// </summary>
|
protected virtual void DoMoveNext(IButtonInfo sender)
|
{
|
if (_SummaryView == null) return;
|
|
_SummaryView.MoveNext();
|
if (tcBusiness.SelectedTabPage != tpSummary) DoViewContent(null);
|
}
|
|
/// <summary>
|
/// 移到最后一条记录
|
/// </summary>
|
protected virtual void DoMoveLast(IButtonInfo sender)
|
{
|
if (_SummaryView == null) return;
|
|
_SummaryView.MoveLast();
|
if (tcBusiness.SelectedTabPage != tpSummary) DoViewContent(null);
|
}
|
|
#endregion
|
|
#region 几个断言
|
|
/// <summary>
|
/// 检查对象是否为空
|
/// </summary>
|
/// <param name="obj">要检测的对象</param>
|
/// <param name="errMsg">如果为空提示信息</param>
|
protected void AssertNull(object obj, string errMsg)
|
{
|
if (null == obj) throw new Exception(errMsg);
|
}
|
|
/// <summary>
|
/// 检查对象对等
|
/// </summary>
|
/// <param name="objA">对象A</param>
|
/// <param name="objB">对象B</param>
|
/// <param name="errMsg">如果不相等提示信息</param>
|
protected void AssertEqual(object objA, object objB, string errMsg)
|
{
|
if (objA != null && objB != null)
|
if (!objA.Equals(objB))
|
throw new Exception(errMsg);
|
}
|
|
/// <summary>
|
/// 检查是否有选择一条记录.
|
/// </summary>
|
protected void AssertFocusedRow()
|
{
|
var ret = _SummaryView == null ||
|
_SummaryView.IsValidRowHandle(_SummaryView
|
.FocusedRowHandle) == false;
|
if (ret) throw new Exception("您没有选择记录,操作取消!");
|
}
|
|
/// <summary>
|
/// 检查数据集是否有数据
|
/// </summary>
|
/// <param name="data"></param>
|
protected void AssertRowNull(DataSet data)
|
{
|
var ret = data == null || data.Tables.Count == 0 ||
|
data.Tables[0].Rows.Count <= 0;
|
if (ret) throw new Exception("该数据集没有数据!");
|
}
|
|
/// <summary>
|
/// 检查数据集是否有数据
|
/// </summary>
|
/// <param name="data"></param>
|
protected void AssertTableEmpty(DataSet ds)
|
{
|
if (ds == null || ds.Tables.Count <= 0 ||
|
ds.Tables[0].Rows.Count <= 0) throw new Exception("该数据集没有数据!");
|
}
|
|
#endregion
|
}
|
}
|