235 lines
8.4 KiB
C#
235 lines
8.4 KiB
C#
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 CommonLib;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Lskj.MyControl {
|
|
public partial class TreeViewEx : UserControl {
|
|
|
|
|
|
|
|
public TreeViewEventHandler MyAfterSelect { get; set; }
|
|
|
|
public TreeViewEx() {
|
|
InitializeComponent();
|
|
tvMain.AfterCheck += new TreeViewEventHandler(tvLeft_AfterCheck);
|
|
tvMain.AfterSelect += new TreeViewEventHandler(tvLeft_AfterSelect);
|
|
}
|
|
[Description("返回树")]
|
|
private TreeView getTreeView;
|
|
public TreeView GetTreeView {
|
|
get {
|
|
return tvMain;
|
|
}
|
|
set { getTreeView = value; }
|
|
}
|
|
|
|
#region 选择树
|
|
protected void tvLeft_AfterSelect(object sender, TreeViewEventArgs e) {
|
|
if (e.Action == TreeViewAction.ByMouse) {
|
|
if (MyAfterSelect != null)
|
|
MyAfterSelect(sender, e);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 绑定树
|
|
protected void tvLeft_AfterCheck(object sender, TreeViewEventArgs e) {
|
|
if (e.Action == TreeViewAction.ByMouse) {
|
|
//取消节点选中状态之后,取消所有父节点的选中状态
|
|
if (e.Node.Checked) {
|
|
setChildNodeCheckedState(e.Node, true);
|
|
}
|
|
else {
|
|
//取消节点选中状态之后,取消所有父节点的选中状态
|
|
setChildNodeCheckedState(e.Node, false);
|
|
//如果节点存在父节点,取消父节点的选中状态
|
|
if (e.Node.Parent != null) {
|
|
setParentNodeCheckedState(e.Node, false);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 取消节点选中状态之后,取消所有父节点的选中状态
|
|
/// </summary>
|
|
/// <param name="currNode"></param>
|
|
/// <param name="state"></param>
|
|
private void setParentNodeCheckedState(TreeNode currNode, bool state) {
|
|
TreeNode parentNode = currNode.Parent;
|
|
parentNode.Checked = state;
|
|
if (currNode.Parent.Parent != null) {
|
|
setParentNodeCheckedState(currNode.Parent, state);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 选中节点之后,选中节点的所有子节点
|
|
/// </summary>
|
|
/// <param name="currNode"></param>
|
|
/// <param name="state"></param>
|
|
private void setChildNodeCheckedState(TreeNode currNode, bool state) {
|
|
TreeNodeCollection nodes = currNode.Nodes;
|
|
if (nodes.Count > 0)
|
|
foreach (TreeNode tn in nodes) {
|
|
tn.Checked = state;
|
|
setChildNodeCheckedState(tn, state);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 替换sql
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static List<string> GetParamValue(string param)
|
|
{
|
|
string SqlParamRegex = "{([^{])+}";
|
|
List<string> list = new List<string>();
|
|
Regex regex = new Regex(SqlParamRegex);
|
|
MatchCollection mcs = regex.Matches(param);
|
|
foreach (Match item in mcs)
|
|
{
|
|
if (list.IndexOf(item.Value) == -1)
|
|
list.Add(item.Value);
|
|
}
|
|
return list;
|
|
}
|
|
public static string ReqSqlParam(string sql, string OperatorId, string OperatorName)
|
|
{
|
|
string sourceSql = sql;
|
|
List<string> listParm = GetParamValue(sql);
|
|
foreach (string s in listParm)
|
|
{
|
|
if (s.IndexOf("loginid", StringComparison.OrdinalIgnoreCase) != -1)
|
|
{
|
|
sourceSql = sourceSql.Replace(s, OperatorId);
|
|
}
|
|
if (s.IndexOf("loginname", StringComparison.OrdinalIgnoreCase) != -1)
|
|
{
|
|
sourceSql = sourceSql.Replace(s, OperatorName);
|
|
}
|
|
if (s.IndexOf("groupid", StringComparison.OrdinalIgnoreCase) != -1)
|
|
{
|
|
sourceSql = sourceSql.Replace(s, ERPInfo.Instance.GroupId);
|
|
}
|
|
}
|
|
return sourceSql;
|
|
}
|
|
|
|
#region 绑定树
|
|
/// <summary>
|
|
/// 绑定树结构
|
|
/// </summary>
|
|
/// <param name="TabName"></param>
|
|
/// <param name="treeView"></param>
|
|
public void BindTree(string DLLCoid) {
|
|
DataTable dtTree = SqlHelper.ExecuteDataSet("select * from p_systemwordbooktab where tab='" + DLLCoid + "' and fieldsqltag=3").Tables[0];
|
|
if (dtTree != null && dtTree.Rows.Count > 0) {
|
|
this.Tag = dtTree.Rows[0];
|
|
string treeSql = dtTree.Rows[0]["fieldsql"] + "";
|
|
treeSql = ReqSqlParam(treeSql,ERPInfo.Instance.EmployeeId.ToString(), ERPInfo.Instance.EmployeeName);
|
|
DataTable dt = SqlHelper.ExecuteDataSet(treeSql).Tables[0];
|
|
for (int i = 0; i < dt.Rows.Count; i++) {
|
|
DataRow dr = dt.Rows[i];
|
|
String tnKey = dr[dtTree.Rows[0]["fieldsqlid"] + ""] + "";
|
|
String tnStr = dr[dtTree.Rows[0]["fieldsqlname"] + ""] + "";
|
|
DataColumnCollection dcc = dt.Columns;
|
|
String CodeNo = string.Empty;
|
|
if (dcc.Count > 2)
|
|
CodeNo = dr[2].ToString();
|
|
TreeNode[] tn = null;
|
|
if (tnKey.Length >= 4) {
|
|
String pKey = tnKey.Substring(0, tnKey.Length - 2);
|
|
tn = tvMain.Nodes.Find(pKey, true);
|
|
if (tn.Count() > 0) {
|
|
TreeNode tNode = tn[0].Nodes.Add(tnKey, tnStr);
|
|
tNode.Tag = CodeNo;
|
|
}
|
|
}
|
|
else {
|
|
TreeNode tNode = tvMain.Nodes.Add(tnKey, tnStr);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private string checkValues = string.Empty;
|
|
/// <summary>
|
|
/// 选中节点之后,选中节点的所有子节点
|
|
/// </summary>
|
|
/// <param name="currNode"></param>
|
|
/// <param name="state"></param>
|
|
private void GetCheckedState(TreeNode currNode) {
|
|
if (currNode.Checked)
|
|
checkValues += "'" + currNode.Name + "',";
|
|
TreeNodeCollection nodes = currNode.Nodes;
|
|
if (nodes.Count > 0) {
|
|
foreach (TreeNode tn in nodes) {
|
|
if (tn.Checked)
|
|
checkValues += "'" + tn.Name + "',";
|
|
GetCheckedState(tn);
|
|
}
|
|
}
|
|
}
|
|
private void GetCheckedStateWithNoQuote(TreeNode currNode)
|
|
{
|
|
if (currNode.Checked)
|
|
checkValues += "" + currNode.Name + ",";
|
|
TreeNodeCollection nodes = currNode.Nodes;
|
|
if (nodes.Count > 0)
|
|
{
|
|
foreach (TreeNode tn in nodes)
|
|
{
|
|
if (tn.Checked)
|
|
checkValues += "" + tn.Name + ",";
|
|
GetCheckedState(tn);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取控件选择值
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string GetCheckValues() {
|
|
checkValues = string.Empty;
|
|
foreach (TreeNode tn in tvMain.Nodes)
|
|
GetCheckedState(tn);
|
|
return checkValues.Trim(',');
|
|
}
|
|
/// <summary>
|
|
/// 获取控件选择值
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string GetCheckValuesWithNoQuote()
|
|
{
|
|
checkValues = string.Empty;
|
|
foreach (TreeNode tn in tvMain.Nodes)
|
|
GetCheckedStateWithNoQuote(tn);
|
|
return checkValues.Trim(',');
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取控件选择值
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public string GetSelectValue() {
|
|
checkValues = string.Empty;
|
|
TreeNode currNode = tvMain.SelectedNode;
|
|
if (currNode != null) {
|
|
checkValues = currNode.Name;
|
|
}
|
|
return checkValues;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|