【Dev TreeList】表记录插入到光标前、后
光标前、后处理逻辑:
/// <summary>
/// 通过导入Excel把数据插入到清单界面
/// 通过导入Excel
/// </summary>
/// <param name="_dtInfo">数据集</param>
/// <param name="_sImportType">导入Excel</param>
/// <param name="_sInsertType">光标前、还是光标后</param>
public void ImportDataByExcel(DataTable _dtInfo, string _sImportType, string _sInsertType)
{
///获取插入点
TreeListNode selectNodeFirst = treeListMain.FocusedNode;
TreeListNode selectNextNodeFirst = null;
if (selectNodeFirst != null)
{
selectNextNodeFirst = selectNodeFirst.NextNode;
}
for (int i = 0; i < _dtInfo.Rows.Count; i++)
{
DataRow row = _dtInfo.Rows[i];
///导入数据-导入Excel数据
///[序号、编号、项目名称、
///[DataSerialNo、DataItemNo、DataItemName、DataItemUnit、UnitCoeff、DataItemCount、DataItemSpec、ItemRemark]
string sSerialNo = row[Pub_Const.c_pfnDataSerialNo].ToString();
string sNumberNo = row[Pub_Const.c_pfnDataItemNo].ToString();
string sDataItemName = row[Pub_Const.c_pfnDataItemName].ToString();
string sInnerCode = row[Pub_Const.c_pfnInnerCode].ToString();
string sParCode = row[Pub_Const.c_pfnParCode].ToString().Trim();
///添加记录
if (string.IsNullOrEmpty(sParCode))
{
treeListMain.Nodes.Add(new object[] {sSerialNo, sNumberNo, sDataItemName, sInnerCode, null});
}
else
{
TreeListNode parentNode = treeListMain.FindNodeByFieldValue(Pub_Const.c_pfnInnerCode, sParCode);
if (parentNode != null)
{
parentNode.Nodes.Add(new object[] { sSerialNo, sNumberNo, sDataItemName, sInnerCode, sParCode});
}
}
///调整节点位置
if (selectNodeFirst == null) continue; ///没有焦点节点, 则不用调整位置
///获取插入点
TreeListNode selectNode = treeListMain.FocusedNode;
int selectNodeIndex = treeListMain.GetNodeIndex(selectNode); ///光标前
if (Pub_Const.c_sBeforeCursor.Equals(_sInsertType))
{
TreeListNode insertNode = treeListMain.FindNodeByFieldValue(Pub_Const.c_pfnInnerCode, sInnerCode);
if (insertNode == null) continue;
treeListMain.BeginUpdate();
treeListMain.SetNodeIndex(insertNode, selectNodeIndex);
treeListMain.EndUpdate();
}
else
{
if (selectNextNodeFirst == null) continue;
int selectNodeNextIndex = treeListMain.GetNodeIndex(selectNextNodeFirst); ///光标后
if (selectNodeNextIndex != -1)
{
TreeListNode insertNode = treeListMain.FindNodeByFieldValue(Pub_Const.c_pfnInnerCode, sInnerCode);
if (insertNode == null) continue;
treeListMain.BeginUpdate();
treeListMain.SetNodeIndex(insertNode, selectNodeNextIndex);
treeListMain.EndUpdate();
}
}
}
}
其中:
一、InnerCode 是TreeList中ID、 ParCode是TreeList中ParentID
二、关键在于SetNodeIndex方法中的Index是相对后一个节点的Index
三、当父节点存在的时候, 要用parentNode.Nodes.Add来添加节点,不然体现不了节点关系.