光标前、后处理逻辑:

光标前处理:获取树节点的focuseNode, 把记录表都插在focuseNode前面
光标后处理:获取树节点的focuseNode的下个节点,并把这个节点保存起来。把这个记录表都插在这个保存起来节点的前面。
这个节点不存在,就不用调整.
 
 
具体代码:
/// <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来添加节点,不然体现不了节点关系.

 

 

 

版权声明:本文为improveself原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/improveself/p/12145983.html