/******************************
* 说明:表格树
* 创建人:龚宇超
* 创建日期:2017-12-26
* 修改人:
* 修改日期:
* 修改备注:
* 版本:1.0.0.0
******************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lskj.Control.Model;
using Lskj.Model;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;
using DevExpress.Utils;
using DevExpress.XtraGrid;
using DevExpress.XtraTreeList.Menu;
using DevExpress.Utils.Menu;
using Lskj.Data;
using Lskj.Business.Impl;
using System.Data.SqlClient;
using Lskj.Util;
using Lskj.Business;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors;
using System.Reflection;
using DevExpress.Data.Filtering;
using DevExpress.XtraGrid.Drawing;
using Lskj.Control.MultiGridLookUp;
using Lskj.Control.MultiModelLookUp;
using System.Text.RegularExpressions;
using Lskj.Core;
using System.Drawing.Drawing2D;
using System.Collections;
namespace Lskj.Control
{
///
/// 表格树
///
public partial class TreeGridControlEx : GridControlEx
{
public TreeList TreeListObj { get { return gcMain; } }
///
/// 树表格固定菜单
///
public ContextMenuStrip MenuStrip { get { return contextMenuStrip; } }
///
/// 默认全部展开
///
public string Expansion = "1";
//treeList树展开的节点块
public List treeListNodes = new List();
///
/// 排序字段
///
public string SortField;
///
/// 上次选中行
///
private int lastNodeIndex;
///
/// 下拉框数据源绑定完成后执行
///
public new event EventHandler OnDataSourceBindCallBack;
public TreeGridControlEx()
{
InitializeComponent();
this.gcMain.KeyDown += new KeyEventHandler(OnTreeGridViewKeyDown);
this.gcMain.SelectionChanged += new EventHandler(gcMain_SelectionChanged);
this.gcMain.CustomDrawNodeCell += new CustomDrawNodeCellEventHandler(OnCustomDrawNodeCell);
this.TreeListObj.CustomDrawNodeIndicator += TreeListObj_CustomDrawNodeIndicator;//显示行号
this.gcMain.CellValueChanged += GcMain_CellValueChanged;
this.gcMain.NodesReloaded += OnNodesReloaded;
if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
{
this.SetGridRowHeightAndFont();
if (SystemInfo.Instance.ExpandButtonType.Equals("1"))
{
this.gcMain.CustomDrawNodeButton += OnCustomDrawNodeButton;//绘制按钮
}
}
//InitializeTreeMethod();
//this.gcMain.OptionsBehavior.EnableFiltering = true;
//this.gcMain.OptionsFind.AlwaysVisible = true;
}
void gcMain_SelectionChanged(object sender, EventArgs e)
{
//try
//{
//List cells = this.gcMain.GetSelectedCells();
//DataRow[] rows = GetViewFocusedDataRows();
//this.pl_buttom.Visible = cells.Count > 1 && rows.Length > 1;
//if (this.pl_buttom.Visible)
//{
// // 分别计算求和、平均值、最大值、最小值、计数
// double sum = 0, avg = 0, max = 0, min = 0;
// int count = 0;
// foreach (DataRow row in rows)
// {
// foreach (TreeListCell cell in cells)
// {
// double cellNumber = 0;
// string cellValue = row[cell.Column.Name] + "";
// if (double.TryParse(cellValue, out cellNumber))
// {
// cellNumber = Convert.ToDouble(cellValue);
// if (max < cellNumber)
// max = cellNumber;
// if (min == 0 || min > cellNumber)
// min = cellNumber;
// count++;
// sum += cellNumber;
// }
// }
// }
// avg = count == 0 || count == 0 ? 0 : sum / count;
// lblTotal.Text = string.Format(ResourceKeys.GridTotalDisplayText, count, Math.Round(sum, 2), Math.Round(avg, 2), Math.Round(max, 2), Math.Round(min, 2));
//}
//}
//catch (Exception)
//{
//}
}
///
/// 说明:设置表格颜色
/// 创建人:龚宇超
/// 创建日期:2017-12-12
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The table.
public override void SetGridRowColors(DataTable table)
{
this.GridRowColorsTable = table;
if (table != null && table.Rows.Count > 0)
{
DataRow rowItem = table.Select("1=1").FirstOrDefault(x => (x["condition"] + "").Contains("COLUMN_"));
if (rowItem != null)
this.gcMain.CustomDrawNodeCell += new CustomDrawNodeCellEventHandler(OnGridViewRowCellStyle);
else
this.gcMain.NodeCellStyle += new GetCustomNodeCellStyleEventHandler(OnGridViewRowStyle);
}
}
///
/// 说明:设置树表格行颜色
/// 创建人:王一帆
/// 创建日期:2019-11-15
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The sender.
/// The instance containing the event data.
protected void OnGridViewRowStyle(object sender, GetCustomNodeCellStyleEventArgs e)
{
if (this.GridRowColorsTable != null && this.GridRowColorsTable.Rows.Count > 0)
{
// 渲染界面数据量过多界面刷新会很慢
DataRowView dataRowView = this.gcMain.GetDataRecordByNode(e.Node) as DataRowView;
DataRow gridRow = dataRowView != null ? dataRowView.Row : null;
foreach (DataRow item in this.GridRowColorsTable.Rows)
{
string cond = ReplaceHelper.ReplaceRowParam(gridRow, item["condition"] + "");
try
{
if (!string.IsNullOrWhiteSpace(cond) && ReplaceHelper.EvalCond(cond))
{
GridRowColorModel model = new GridRowColorModel(item);
if (!string.IsNullOrWhiteSpace(model.BackColor))
{
e.Appearance.BackColor = ColorTranslator.FromHtml(model.BackColor);
}
if (!string.IsNullOrWhiteSpace(model.ForceColor))
{
e.Appearance.ForeColor = ColorTranslator.FromHtml(model.ForceColor);
}
FontStyle fontStyle = new FontStyle();
if (model.IsBold) fontStyle = fontStyle | FontStyle.Bold;
if (model.IsItalic) fontStyle = fontStyle | FontStyle.Italic;
if (model.IsStrickOut) fontStyle = fontStyle | FontStyle.Strikeout;
if (model.IsUnderLine) fontStyle = fontStyle | FontStyle.Underline;
e.Appearance.Font = new Font(e.Appearance.Font, fontStyle);
}
}
catch (Exception)
{
//LogHelper.Instance.WriteLog("GridView RowCellStyle Error. Condition:" + model.Condition + ";EvalCondtion:" + cond);
}
}
}
}
///
/// 说明:设置树表格单元格颜色
/// 创建人:王一帆
/// 创建日期:2019-11-15
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The source of the event.
/// The instance containing the event data.
///
protected void OnGridViewRowCellStyle(object sender, CustomDrawNodeCellEventArgs e)
{
if (this.GridRowColorsTable != null && this.GridRowColorsTable.Rows.Count > 0 && !string.IsNullOrEmpty(e.CellValue + ""))
{
// 渲染界面数据量过多界面刷新会很慢
DataRowView dataRowView = this.gcMain.GetDataRecordByNode(e.Node) as DataRowView;
DataRow gridRow = dataRowView != null ? dataRowView.Row : null;
foreach (DataRow item in this.GridRowColorsTable.Rows)
{
string cond = ReplaceHelper.ReplaceRowParam(gridRow, item["condition"] + "").ReplaceColumnParam(e.Column.Name, e.Column.Caption, e.CellValue + "");
try
{
if (!string.IsNullOrWhiteSpace(cond) && ReplaceHelper.EvalCond(cond))
{
GridRowColorModel model = new GridRowColorModel(item);
if (!string.IsNullOrWhiteSpace(model.BackColor))
{
e.Appearance.BackColor = ColorTranslator.FromHtml(model.BackColor);
}
if (!string.IsNullOrWhiteSpace(model.ForceColor))
{
e.Appearance.ForeColor = ColorTranslator.FromHtml(model.ForceColor);
}
FontStyle fontStyle = new FontStyle();
if (model.IsBold) fontStyle = fontStyle | FontStyle.Bold;
if (model.IsItalic) fontStyle = fontStyle | FontStyle.Italic;
if (model.IsStrickOut) fontStyle = fontStyle | FontStyle.Strikeout;
if (model.IsUnderLine) fontStyle = fontStyle | FontStyle.Underline;
e.Appearance.Font = new Font(e.Appearance.Font, fontStyle);
}
}
catch (Exception)
{
//LogHelper.Instance.WriteLog("GridView RowCellStyle Error. Condition:" + model.Condition + ";EvalCondtion:" + cond);
}
}
}
}
///
/// 绘制按钮
///
///
///
private void OnCustomDrawNodeButton(object sender, CustomDrawNodeButtonEventArgs e)
{
Rectangle rect = Rectangle.Inflate(e.Bounds, 0, 0);
Image img = e.Expanded ? global::Lskj.Control.Properties.Resources.UnExpand : global::Lskj.Control.Properties.Resources.Expand;
float scaleX = (float)rect.Width / img.Width;
float scaleY = (float)rect.Height / img.Height;
float scale = Math.Min(scaleX, scaleY);
int newWidth = (int)(img.Width * scale);
int newHeight = (int)(img.Height * scale);
int offsetX = (rect.Width - newWidth) / 2;
int offsetY = (rect.Height - newHeight) / 2;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.DrawImage(img, rect.X + offsetX, rect.Y + offsetY, newWidth, newHeight);
e.Handled = true;
}
///
/// 节点加载完成后
///
///
///
private void OnNodesReloaded(object sender, EventArgs e)
{
//根据记录展开对应节点
foreach (TreeListNode treeListNode in treeListNodes)
{
TreeListNode treeListNode1 = this.gcMain.Nodes.TreeList.FindNodeByID(treeListNode.Id);
if (treeListNode1 != null) treeListNode1.Expanded = true;
}
this.TreeListObj.FocusedNode = this.TreeListObj.GetNodeByVisibleIndex(lastNodeIndex);
if ((Expansion + "").Equals("1"))
{
this.gcMain.ExpandAll();
}
else if ((Expansion + "").Equals("2"))
{
this.gcMain.ExpandToLevel(0);
}
else if ((Expansion + "").Equals("3"))
{
this.gcMain.CollapseAll();
}
//if (selectRowHandler)
//{
// this.TreeListObj.FocusedNode = this.TreeListObj.GetNodeByVisibleIndex(lastNodeIndex);
//}
}
///
/// 说明:设置只读列
/// 创建人:龚宇超
/// 创建日期:2018-03-30
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// The table.
/// The custom colum key.
public override void SetReadOnlyColumns(DataTable table, string customColumKey = "")
{
if (table == null) return;
Dictionary