TreeView----从数据库取数据构建TreeView树

简介:医院有很多工作单位,"" --->医院   code 是2位(如 10,20等)代表是医院的子节点,code是4位(如 1012,1011)是 10的子节点,以此类推;用递归的方法用这些数据构建TreeView树。 效果图如下:

 2011012021530585.png

 代码如下:   

ContractedBlock.gif ExpandedBlockStart.gif 构建TreeView的xaml
 
   
< Window x:Class = " CreateTree.MainWindow "
xmlns
= " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x
= " http://schemas.microsoft.com/winfx/2006/xaml "
Title
= " MainWindow " Height = " 429 " Width = " 331 " >
< Grid Height = " 391 " >
< TreeView Height = " 274 " HorizontalAlignment = " Left " Margin = " 12,12,0,0 " Name = " tvOrg " VerticalAlignment = " Top " Width = " 174 " />
</ Grid >
</ Window >

 

ContractedBlock.gif ExpandedBlockStart.gif 构建TreeView cs文件
 
   
namespace CreateTree
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();


// 根结点
TreeOrgDict treeDict = new TreeOrgDict();
treeDict.OrgCode
= "" ;
treeDict.OrgName
= " 医院单位 " ;

List
< OrgDict > orgList = GetOrgList(); // 递归多次调用方法GetNodeList,所以要放在方法的外面作为参数

// 获取根结点的子节点
treeDict.SubList = GetNodeList( "" , orgList, 0 );

TreeViewItem newItem
= new TreeViewItem() { Tag = treeDict, Header = treeDict.OrgName };
newItem.IsExpanded
= true ;
if (treeDict.SubList != null )
{
// 递归赋值
SetTreeNodes(treeDict.SubList.ToList(), newItem);
}
this .tvOrg.Items.Add(newItem);

}

// 递归算法组装树
public List < TreeOrgDict > GetNodeList( string orgcode, List < OrgDict > orgList, int num)
{

int loop = num + 2 ; // 子结点code的位数
List < OrgDict > rootNoods = new List < OrgDict > ();
List
< TreeOrgDict > reList = new List < TreeOrgDict > ();
if (orgcode == "" )
{
// 根结点
rootNoods = (from org in orgList
where org.OrgCode.Length == 2
select org).ToList();
}
else
{
// 非根结点
rootNoods = (from org in orgList
where org.OrgCode.Length == loop &&
org.OrgCode.Substring(
0 , num).Equals(orgcode)
select org).ToList();
}

// 递归出口
if (rootNoods.Count() == 0 )
{
return null ;
}
else
{
foreach (var node in rootNoods)
{
TreeOrgDict treedict
= new TreeOrgDict();
treedict.OrgCode
= node.OrgCode;
treedict.OrgName
= node.OrgName;

treedict.SubList
= GetNodeList(node.OrgCode, orgList, loop);

reList.Add(treedict);
}
}
return reList;

}

// 递归给树赋值
public void SetTreeNodes(List < TreeOrgDict > treeSubList, TreeViewItem newItem)
{
foreach (var node in treeSubList)
{
TreeViewItem subItem
= new TreeViewItem() { Tag = node, Header = node.OrgName };

if (node.SubList != null )
{
SetTreeNodes(node.SubList.ToList(), subItem);
}

newItem.Items.Add(subItem);
}
}

// 获取单位的列表
public List < OrgDict > GetOrgList()
{
List
< OrgDict > orgList = new List < OrgDict > ();
OrgDict org10
= new OrgDict( " 10 " , " 政治部 " );
OrgDict org1001
= new OrgDict( " 1001 " , " 政治部办公室 " );
OrgDict org1002
= new OrgDict( " 1002 " , " 组织处 " );
OrgDict org1003
= new OrgDict( " 1003 " , " 干部处 " );
OrgDict org11
= new OrgDict( " 11 " , " 内科临床部 " );
OrgDict org1101
= new OrgDict( " 1101 " , " 内分泌科师干病室 " );
OrgDict org1102
= new OrgDict( " 1102 " , " 门诊内窥镜室 " );
OrgDict org16
= new OrgDict( " 16 " , " 临床部 " );
OrgDict org1601
= new OrgDict( " 1601 " , " 南楼二科 " );
OrgDict org1602
= new OrgDict( " 1602 " , " 南楼四科 " );
OrgDict org1603
= new OrgDict( " 1603 " , " 南楼六科 " );
OrgDict org20
= new OrgDict( " 20 " , " 院首长 " );
OrgDict org2010
= new OrgDict( " 2010 " , " 院长办公室 " );
OrgDict org2012
= new OrgDict( " 2012 " , " 副院长办公室 " );
OrgDict org2016
= new OrgDict( " 2016 " , " 党委书记办公室 " );
OrgDict org2018
= new OrgDict( " 2018 " , " 副党委书记办公室 " );

orgList.Add(org10);
orgList.Add(org1001);
orgList.Add(org1002);
orgList.Add(org1003);
orgList.Add(org11);
orgList.Add(org1101);
orgList.Add(org1102);
orgList.Add(org16);
orgList.Add(org1601);
orgList.Add(org1602);
orgList.Add(org1603);
orgList.Add(org20);
orgList.Add(org2010);
orgList.Add(org2012);
orgList.Add(org2016);
orgList.Add(org2018);

return orgList;
}
}


// 单位类
public class OrgDict
{
// 单位编号
private string orgcode;

public string OrgCode
{
get { return orgcode; }
set { orgcode = value; }
}

// 单位名称
private string orgname;

public string OrgName
{
get { return orgname; }
set { orgname = value; }
}

// 构造方法
public OrgDict( string code, string name)
{
this .orgcode = code;
this .orgname = name;
}
}

// 单位结点类
public class TreeOrgDict
{
// 单位编号
private string orgcode;

public string OrgCode
{
get { return orgcode; }
set { orgcode = value; }
}

// 单位名称
private string orgname;

public string OrgName
{
get { return orgname; }
set { orgname = value; }
}

// 字节点列表
private List < TreeOrgDict > subList;

public List < TreeOrgDict > SubList
{
get { return subList; }
set { subList = value; }
}
}

}

 

转载于:https://www.cnblogs.com/shuming/archive/2011/01/20/1940628.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果调用`treeView->setModel(index.model())`报错,可能是因为缺少`QTreeView`的模型。你可以在创建`QTreeView`时,同时创建一个空的`QStandardItemModel`,在`setEditorData()`方法中将`QStandardItemModel`设置为`QTreeView`的模型。 修改后的代码如下所示: ```cpp class TreeDelegate : public QStyledItemDelegate { public: QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const override { QTreeView* treeView = new QTreeView(parent); treeView->setModel(new QStandardItemModel(treeView)); return treeView; } void setEditorData(QWidget* editor, const QModelIndex& index) const override { QTreeView* treeView = static_cast<QTreeView*>(editor); QStandardItemModel* model = static_cast<QStandardItemModel*>(treeView->model()); model->clear(); // 清空模型中的数据 treeView->setRootIndex(index); treeView->expandAll(); } void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override { QTreeView* treeView = static_cast<QTreeView*>(editor); QModelIndexList selectionList = treeView->selectionModel()->selectedIndexes(); QModelIndex selectionIndex = selectionList.at(0); QVariant data = selectionIndex.data(Qt::DisplayRole); model->setData(index, data); } void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const override { editor->setGeometry(option.rect); } }; ``` 在`createEditor()`方法中,同时创建了一个空的`QStandardItemModel`并将其设置为`QTreeView`的模型。 在`setEditorData()`方法中,每次调用该方法时先清空模型中的数据,再将根索引设置为传入的索引,并展开所有项。 这样就可以避免`treeView->setModel(index.model())`报错了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值