- 积分
- 33
- 在线时间
- 53 小时
- 主题
- 28
- 注册时间
- 2013-8-31
- 帖子
- 132
- 最后登录
- 2019-10-21
- 帖子
- 132
- 软币
- 3320
- 在线时间
- 53 小时
- 注册时间
- 2013-8-31
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraPrinting;
namespace Nbjjy
{
public partial class BaseForm : DevExpress.XtraEditors.XtraForm
{
public BaseForm()
{
InitializeComponent();
}
#region 系统功能
/// <summary>
/// 系统初始载入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BaseForm_Load(object sender, EventArgs e)
{
if (!base.DesignMode)
{
this.Cursor = Cursors.WaitCursor; //等待状态
try
{
this.FormOnLoad();
}
catch (Exception ex)
{
this.ProcessException(ex);
}
finally
{
this.Cursor = Cursors.Default;
}
}
}
/// <summary>
/// 初始化载入
/// </summary>
public virtual void FormOnLoad(){}
/// <summary>
/// 写入错误信息
/// </summary>
/// <param name="ex"></param>
public void WriteException(Exception ex)
{
//未写,后期再写
DictItemUtil.AddLog(ex.Message);
}
/// <summary>
/// 错误处理
/// </summary>
/// <param name="ex"></param>
public void ProcessException(Exception ex)
{
this.WriteException(ex);
MessageUtil.ShowError(ex.Message);
}
/// <summary>
/// TAB切换
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BaseForm_KeyUp(object sender, KeyEventArgs e)
{
Keys keyCode = e.KeyCode;
if (keyCode == Keys.F5)
{
this.FormOnLoad();
}
else if (((e.KeyCode == Keys.Return) || (e.KeyCode == Keys.Return)) && (((base.ActiveControl is TextEdit) || (base.ActiveControl is MemoEdit) || (base.ActiveControl is DevExpress.XtraEditors.ComboBoxEdit)) || (base.ActiveControl is CheckEdit)))
{
if (base.ActiveControl is TextEdit)
{
return;
}
SendKeys.Send("{TAB}");
}
}
#endregion
#region 复制行信息
/// <summary>
/// 复制行信息
/// </summary>
/// <param name="gridView1">GridView</param>
public void CopyData(DevExpress.XtraGrid.Views.Grid.GridView gridView1)
{
StringBuilder stringBuilder = new StringBuilder();
StringBuilder stringBuilder2 = new StringBuilder();
foreach (GridColumn gridColumn in gridView1.Columns)
{
if (gridColumn.Visible)
{
stringBuilder.AppendFormat("{0} ", gridColumn.Caption);
}
}
int[] selectedRows = gridView1.GetSelectedRows();
int[] array = selectedRows;
for (int i = 0; i < array.Length; i++)
{
int rowHandle = array[i];
foreach (GridColumn gridColumn in gridView1.Columns)
{
if (gridColumn.Visible)
{
stringBuilder2.AppendFormat("{0} ", gridView1.GetRowCellDisplayText(rowHandle, gridColumn.FieldName));
}
}
stringBuilder2.AppendLine();
}
Clipboard.SetText(stringBuilder.ToString() + "\r\n" + stringBuilder2.ToString());
}
#endregion
#region 自定义显示列
/// <summary>
/// 自定义显示列
/// </summary>
/// <param name="gridView1">GridView</param>
public void CustomColumn(DevExpress.XtraGrid.Views.Grid.GridView gridView1)
{
new CustomColumnDialog(gridView1.Columns).ShowDialog();
}
#endregion
#region 导出Excel
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="gridControl">GridControl</param>
/// <param name="fileName">导出文件名</param>
public void ExportExcel(DevExpress.XtraGrid.GridControl gridControl, string fileName)
{
if (gridControl.DataSource != null)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = "导出Excel";
saveFileDialog.Filter = "Excel文件(*.xls)|*.xls";
saveFileDialog.FileName = fileName;
DialogResult dialogResult = saveFileDialog.ShowDialog(this);
if (dialogResult == DialogResult.OK)
{
gridControl.ExportToXls(saveFileDialog.FileName);
MessageUtil.ShowTips("保存成功!");
}
}
else
{
MessageUtil.ShowError("没有数据,系统无法为你导出信息!");
}
}
#endregion
#region 打印功能
/// <summary>
/// 打印功能
/// </summary>
/// <param name="gridControl">GridControl</param>
/// <param name="PrintTitle">打印标题</param>
public void ExportPrint(DevExpress.XtraGrid.GridControl gridControl, string PrintTitle)
{
PrintingSystem printingSystem = new PrintingSystem();
PrintableComponentLink printableComponentLink = new PrintableComponentLink();
string[] value = new string[]
{
"",
PrintTitle,
""
};
string[] value2 = new string[]
{
"",
"[page # of pages #]",
"[Date Printed][Time Printed]"
};
PageHeaderFooter pageHeaderFooter = new PageHeaderFooter();
pageHeaderFooter.Header.Content.AddRange(value);
pageHeaderFooter.Header.Font = new Font("隶书", 18f);
pageHeaderFooter.Header.LineAlignment = BrickAlignment.Center;
pageHeaderFooter.Footer.Content.AddRange(value2);
printableComponentLink.PaperKind = printingSystem.PageSettings.PaperKind;
printableComponentLink.Landscape = printingSystem.PageSettings.Landscape;
printableComponentLink.PageHeaderFooter = pageHeaderFooter;
printableComponentLink.Component = gridControl;
printableComponentLink.CreateDocument(printingSystem);
printingSystem.PageSettings.TopMargin = 100;
printingSystem.PageSettings.LeftMargin = 40;
printingSystem.PageSettings.RightMargin = 40;
printingSystem.PageSettings.BottomMargin = 80;
printingSystem.SetCommandVisibility(PrintingSystemCommand.Customize, CommandVisibility.None);
printingSystem.SetCommandVisibility(PrintingSystemCommand.PageLayout, CommandVisibility.None);
printingSystem.SetCommandVisibility(PrintingSystemCommand.Watermark, CommandVisibility.None);
printingSystem.SetCommandVisibility(PrintingSystemCommand.EditPageHF, CommandVisibility.None);
printingSystem.SetCommandVisibility(PrintingSystemCommand.ExportFile, CommandVisibility.None);
printingSystem.SetCommandVisibility(PrintingSystemCommand.SendFile, CommandVisibility.None);
printingSystem.PreviewFormEx.Text = "打印预览";
printingSystem.PreviewFormEx.Owner = gridControl.FindForm();
printingSystem.PreviewFormEx.Show();
}
#endregion
#region GridControl常用属性
/// <summary>
/// GridControl常用属性
/// </summary>
/// <param name="gridControl">GridControl属性</param>
/// <param name="gridView1">GridView属性</param>
/// <param name="AutoWidth">是否自动列宽</param>
/// <param name="ShowLineNumber">是否显示行号</param>
/// <param name="ShowNavigator">是否显示自带的分页功能</param>
public void GridControlOnLoad(DevExpress.XtraGrid.GridControl gridControl, DevExpress.XtraGrid.Views.Grid.GridView gridView1, bool AutoWidth, bool ShowLineNumber, bool ShowNavigator)
{
gridView1.RowHeight = 20;
gridView1.ColumnPanelRowHeight = 22;
gridView1.FooterPanelHeight = 20;
gridView1.OptionsMenu.EnableColumnMenu = false;
gridView1.OptionsMenu.EnableFooterMenu = false;
gridView1.OptionsMenu.EnableGroupPanelMenu = false;
gridView1.OptionsNavigation.AutoFocusNewRow = true;
gridView1.OptionsView.ColumnAutoWidth = AutoWidth;
gridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;
gridView1.OptionsView.ShowFooter = false;
gridView1.OptionsView.ShowGroupPanel = false;
gridView1.OptionsView.EnableAppearanceEvenRow = true;
gridView1.OptionsView.EnableAppearanceOddRow = true;
gridView1.OptionsView.ShowGroupExpandCollapseButtons = false;
gridView1.OptionsCustomization.AllowFilter = false;
gridView1.OptionsCustomization.AllowGroup = false;
gridView1.OptionsBehavior.Editable = false;
gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.False;
gridView1.OptionsBehavior.AllowDeleteRows = DevExpress.Utils.DefaultBoolean.False;
gridView1.OptionsBehavior.ReadOnly = true;
gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
gridView1.OptionsSelection.MultiSelect = true;
gridView1.OptionsDetail.EnableMasterViewMode = false;
gridView1.OptionsPrint.AutoWidth = AutoWidth;
gridView1.OptionsPrint.UsePrintStyles = true;
gridView1.OptionsLayout.Columns.StoreAllOptions = true;
gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.None;
gridView1.AppearancePrint.Preview.BackColor = Color.White;
gridView1.AppearancePrint.HeaderPanel.BackColor = Color.White;
gridView1.AppearancePrint.FooterPanel.BackColor = Color.White;
gridView1.AppearancePrint.GroupRow.BackColor = Color.White;
gridView1.AppearancePrint.GroupFooter.BackColor = Color.White;
gridView1.Appearance.EvenRow.BackColor = System.Drawing.Color.LightCyan;
gridView1.Appearance.EvenRow.Options.UseBackColor = true;
if (ShowLineNumber)
{
gridView1.IndicatorWidth = 42;
gridView1.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(gridView1_CustomDrawRowIndicator);
}
if (ShowNavigator)
{
gridControl.EmbeddedNavigator.Buttons.CancelEdit.Visible = false;
gridControl.EmbeddedNavigator.Buttons.Edit.Visible = false;
gridControl.EmbeddedNavigator.Buttons.EndEdit.Visible = false;
gridControl.EmbeddedNavigator.Buttons.Append.Visible = false;
gridControl.EmbeddedNavigator.Buttons.Remove.Visible = false;
gridControl.UseEmbeddedNavigator = true;
}
}
/// <summary>
/// 显示行号
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
if (e.Info.IsRowIndicator)
{
if (e.RowHandle >= 0)
{
e.Info.DisplayText = (e.RowHandle + 1).ToString();
}
}
}
#endregion
#region TreeList的常用属性
/// <summary>
/// TreeList的常用属性
/// </summary>
/// <param name="treeList1">TreeList属性</param>
public void TreeListOnLoad(DevExpress.XtraTreeList.TreeList treeList1,bool check)
{
treeList1.OptionsMenu.EnableColumnMenu = false;
treeList1.OptionsMenu.EnableFooterMenu = false;
treeList1.OptionsView.ShowIndicator = false;
treeList1.OptionsView.ShowHorzLines = false;
treeList1.OptionsView.ShowVertLines = false;
treeList1.OptionsBehavior.Editable = false;
treeList1.OptionsBehavior.AutoFocusNewNode = true;
treeList1.Appearance.FocusedCell.BackColor = System.Drawing.Color.LightCyan;
treeList1.Appearance.FocusedCell.BackColor2 = System.Drawing.Color.LightCyan;
treeList1.Appearance.FocusedCell.Options.UseBackColor = true;
if (check)
{
treeList1.OptionsView.ShowCheckBoxes = true;
treeList1.OptionsBehavior.AllowIndeterminateCheckState = true;
treeList1.BeforeCheckNode += new DevExpress.XtraTreeList.CheckNodeEventHandler(treeList1_BeforeCheckNode);
treeList1.AfterCheckNode += new DevExpress.XtraTreeList.NodeEventHandler(treeList1_AfterCheckNode);
}
treeList1.ExpandAll();
}
/// <summary>
/// 全选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{
SetCheckedChildNodes(e.Node, e.Node.CheckState);
SetCheckedParentNodes(e.Node, e.Node.CheckState);
}
/// <summary>
/// 反选
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
{
e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
}
/// <summary>
/// 设置子节点的状态
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
node.Nodes[i].CheckState = check;
SetCheckedChildNodes(node.Nodes[i], check);
}
}
/// <summary>
/// 设置父节点的状态
/// </summary>
/// <param name="node"></param>
/// <param name="check"></param>
private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
{
if (node.ParentNode != null)
{
bool b = false;
for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
{
CheckState state = (CheckState)node.ParentNode.Nodes[i].CheckState;
if (!check.Equals(state))
{
b = !b;
break;
}
}
node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;
SetCheckedParentNodes(node.ParentNode, check);
}
}
#endregion
}
}
|
|