重点是把要放Tree的那个单元格所在的列用一个全局变量保存,在事件中好给它赋值
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinDataSource;
using Infragistics.Win.UltraWinEditors;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinTree;
using Jurassic.RMSys.Infrastructure.Library;
namespace Jurassic.RMSys.BaseDataManage.VersionAssessment
{
public partial class VersionManager : Form
{
public VersionManager()
{
InitializeComponent();
}
#region 全量变量
//用来保存有嵌入树的那一列
public UltraGridColumn TreeColumn;
//用来保存嵌入树那一列的UltraTextEditor
public UltraTextEditor TextEditor;
//用来保存组织结构的内存表
public DataTable TbOrganization = ClientDataProvider.Service.SelectSqlCommon("T_Register_Organization");
#endregion
//新增一个Grid的空白行,并给予默认值
private void tsbAdd_Click(object sender, EventArgs e)
{
UltraGridRow row = ulgVersion.DisplayLayout.Bands[0].AddNew();
row.Cells["StartTime"].Value = DateTime.Now;
row.Cells["UserID"].Value = AppGlobal.UserFullName;
row.Cells["UserID"].Activation = Activation.NoEdit;
row.Cells["BaseDay"].Value = DateTime.Now;
ulgVersion.ActiveRow = row;
ulgVersion.DisplayLayout.Bands[0].PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
}
//删除选中行
private void tsbDelete_Click(object sender, EventArgs e)
{
if (ulgVersion.ActiveRow != null && MessageBox.Show("确定要删除选定行吗?", "提示",
MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
var workID = ulgVersion.ActiveRow.Cells["WorkID"].Value;
if (workID != null && !String.IsNullOrEmpty(workID + ""))
{
var parameterObject = new List<Tuple<string, string, object>>
{
new Tuple<string, string, object>("WorkID", "=", workID),
};
ClientDataProvider.Service.DeleteSqlCommon("T_Work", parameterObject);
}
ulgVersion.ActiveRow.Delete(false);
}
}
//确定并选择当前计算的版本
private void btnOK_Click(object sender, EventArgs e)
{
if (ulgVersion.ActiveRow == null)
{
MessageBox.Show("请选择要计算的版本!");
return;
}
//创建一个内存表,防止插入计算日期和计算人相同的记录
var checkInsertTable = new DataTable();
checkInsertTable.Columns.Add("WorkYear", typeof (DateTime));
checkInsertTable.Columns.Add("UserID", typeof(string));
foreach (UltraDataRow row in ultraDataSource1.Rows)
{
//var findYearUserID =
// checkInsertTable.Select("WorkYear = " + row["WorkYear"] + " and UserID = " + row["UserID"]);
//if (findYearUserID.Length > 0)
//{
// MessageBox.Show("不能提交日期和计算人相同的两条或两条以上的记录");
// LoadGridDataSource();
// return;
//}
//else
//{
// var newRow = checkInsertTable.NewRow();
// newRow["WorkYear"] = row["WorkYear"];
// newRow["UserID"] = row["UserID"];
// checkInsertTable.Rows.Add(newRow);
//}
}
#region 插入版本记录
foreach (UltraDataRow row in ultraDataSource1.Rows)
{
if (row["WorkID"] + "" == "")
{
var parameterObject = new Dictionary<string, object>();
foreach (UltraDataColumn col in
row.Band.Columns.Cast<UltraDataColumn>().Where(col => col.Key != "WorkID"))
{
if (String.IsNullOrEmpty(row[col.Key] + ""))
{
var headerCaption = ulgVersion.DisplayLayout.Bands[0].Columns[col.Key].Header.Caption;
MessageBox.Show("请填写'" + headerCaption + "'再提交!");
return;
}
switch (col.Key)
{
case "OrganizationID":
if (TbOrganization.Rows.Count > 0)
{
var findRows = TbOrganization.Select("OrganizationName = '" + row[col.Key] + "'");
if (findRows.Length > 0)
{
parameterObject.Add(col.Key, findRows[0][col.Key]);
}
}
break;
case "UserID":
parameterObject.Add(col.Key, AppGlobal.USERID);
break;
default:
{
DateTime ds;
parameterObject.Add(col.Key,
DateTime.TryParse(row[col.Key] + "", out ds) ? ds : row[col.Key]);
}
break;
}
}
parameterObject.Add("WorkPID", 0);
ClientDataProvider.Service.InsertSqlCommon("t_work", parameterObject);
}
}
#endregion
#region 得到当前选择的评估版本ID
var workID = ulgVersion.ActiveRow.Cells["WorkID"].Value;
if (workID != null && !String.IsNullOrEmpty(workID + ""))
{
AppGlobal.WorkID = workID + "";
}
else
{
DateTime ds;
//反查刚刚插入的数据,得到ID(万一选择的是新增的版本的话)
var parameterWhere = new List<Tuple<string, string, object>>
{
new Tuple<string, string, object>("WorkYear", "=",
DateTime.TryParse(
ulgVersion.ActiveRow.Cells["WorkYear"].Value + "", out ds) ? ds :
ulgVersion.ActiveRow.Cells["WorkYear"].Value),
new Tuple<string, string, object>("UserID","=",
AppGlobal.USERID)
};
var tWorkTable = ClientDataProvider.Service.SelectSqlCommon("T_Work", null, parameterWhere);
if (tWorkTable.Rows.Count > 0)
{
AppGlobal.WorkID = tWorkTable.Rows[0]["WorkID"] + "";
}
}
#endregion
//做完所有操作后,关闭窗体
Close();
}
//在Grid初始化事件中给单元格嵌入组织机构树,并注册下拉框事件
private void ulgVersion_InitializeLayout(object sender, InitializeLayoutEventArgs e)
{
var vl = new ValueList();
vl.AfterDropDownResize += vl_AfterDropDownResize;
var workTypeTable = ClientDataProvider.Service.SelectSqlCommon("dm_worktype");
foreach (DataRow loopRow in workTypeTable.Rows)
{
vl.ValueListItems.Add(loopRow["WorkTypeID"], loopRow["WorkTypeName"] + "");
}
ulgVersion.DisplayLayout.Bands[0].Columns["StartTime"].Style =
Infragistics.Win.UltraWinGrid.ColumnStyle.Date;
ulgVersion.DisplayLayout.Bands[0].Columns["BaseDay"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Date;
ulgVersion.DisplayLayout.Bands[0].Columns["WorkYear"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.Date;
var treeTarget = new UltraTree();
#region 生成组织结构树
var rootRootNode = new UltraTreeNode("rootnode", "中国石油天然气股份有限公司");
treeTarget.Nodes.Add(rootRootNode);
if (TbOrganization.Rows.Count > 0)
{
foreach (DataRow row in TbOrganization.Rows)
{
if (row["OrganizationPID"] + "" == "0")
{
var rootNode = new UltraTreeNode(row["OrganizationID"] + "", row["OrganizationName"] + "");
rootRootNode.Nodes.Add(rootNode);
var findRows = TbOrganization.Select("OrganizationPID = '" + row["OrganizationID"] + "'");
if (findRows.Length > 0)
{
foreach (DataRow childRow in findRows)
{
//采油厂结点
var childNode = new UltraTreeNode(childRow["OrganizationID"] + "",
childRow["OrganizationName"] + "");
rootNode.Nodes.Add(childNode);
}
}
}
}
}
#endregion
treeTarget.ExpandAll();
#region 把树绑定到Grid的某列的单元格中
var textEditor = new UltraTextEditor();
var button = new DropDownEditorButton { Control = treeTarget };
textEditor.ButtonsRight.Add(button);
ulgVersion.DisplayLayout.Bands[0].Columns["OrganizationID"].EditorComponent = textEditor;
#endregion
//把绑定树这一列赋给一个全局变量
TreeColumn = ulgVersion.DisplayLayout.Bands[0].Columns["OrganizationID"];
//注册下拉框事件
textEditor.AfterEditorButtonCloseUp += textEditor_AfterEditorButtonCloseUp;
//双击树节点,触发关闭下拉框的事件
treeTarget.DoubleClick += treeTarget_DoubleClick;
ulgVersion.DisplayLayout.Bands[0].Columns["WorkTypeID"].Style =
Infragistics.Win.UltraWinGrid.ColumnStyle.DropDown;
ulgVersion.DisplayLayout.Bands[0].Columns["WorkTypeID"].ValueList = vl;
TextEditor = textEditor;
}
void vl_AfterDropDownResize(object sender, EventArgs e)
{
ulgVersion.DisplayLayout.Bands[0].PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
}
//双击树结点触发下拉框关闭事件,事件触发时把结点的值放到Grid的单元格中
void treeTarget_DoubleClick(object sender, EventArgs e)
{
TextEditor.CloseEditorButtonDropDowns();
}
//在下拉框事件中把选中的树节点的值赋给所属单元格
private void textEditor_AfterEditorButtonCloseUp(object sender, EditorButtonEventArgs e)
{
var textEditor = sender as UltraTextEditor;
var button = e.Button as DropDownEditorButton;
if (button != null)
{
var tree = (UltraTree)button.Control;
if (tree.ActiveNode != null && textEditor != null)
{
TreeColumn.Layout.ActiveRow.Cells["OrganizationID"].Value = tree.ActiveNode.Text;
}
}
//重新自适应列宽
ulgVersion.DisplayLayout.Bands[0].PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
}
//窗体加载时,加载评估工作数据到Grid中
private void VersionManager_Load(object sender, EventArgs e)
{
LoadGridDataSource();
//如果存在版本记录,那么自适列宽,并把单元格设为不可编辑
if (ultraDataSource1.Rows.Count > 0)
{
ulgVersion.DisplayLayout.Bands[0].PerformAutoResizeColumns(true, PerformAutoSizeType.VisibleRows);
foreach (UltraGridRow row in ulgVersion.Rows)
foreach (UltraGridColumn col in ulgVersion.DisplayLayout.Bands[0].Columns)
row.Cells[col.Key].Activation = Activation.NoEdit;
}
}
private void LoadGridDataSource()
{
//得到"评估工作"表
var tWorkTable = ClientDataProvider.Service.SelectSqlCommon("T_Work");
#region 把数据库存查询的DataTable绑定到ultraDataSource
foreach (DataRow loopRow in tWorkTable.Rows)
{
UltraDataRow dr = ultraDataSource1.Rows.Add();
foreach (var colName in
tWorkTable.Columns.Cast<DataColumn>().Select(loopCol =>
loopCol.Caption).Where(colName => dr.Band.Columns.Exists(colName)))
{
switch (colName)
{
case "OrganizationID":
{
var findRows = TbOrganization.Select("OrganizationID = '" + loopRow[colName] + "'");
if (findRows.Length > 0)
dr[colName] = findRows[0]["OrganizationName"];
}
break;
case "UserID":
dr[colName] = AppGlobal.USERNAME;
break;
default:
dr[colName] = loopRow[colName];
break;
}
}
}
#endregion
}
}
}