網上一直有朋友說.net上的treeview不夠快,而且也不方便。那麼真實的情況是否如此呢。我做的一專案中需要乙個快速的tree,它的資料來源是ms sql,其對應表中記錄的結構如下: id
fatherid
title
0101 01
基本建設支出
010109
0101
其它基本建設支出
其特點是父節點的id正好是當前記錄id的長度-2所擷取的字串
因此我寫下了如下**:
一、普通的方法
private
function buildentree(byval ds as dataset)
dim rs as datarowcollection
dim r as datarow
dim id as
string
dim fatherid as
string
dim title as
string
dim fn as treenode
dim node as treenode
rs = ds.tables(0).rows
bootnode = new treenode
bootnode.text = "[0] 所有單位"
bootnode.tag = "0" '根目錄
treeen.nodes.add(bootnode)
foreach r in rs
id = directcast(r.item(0), string).trim 'code
fatherid = directcast(r.item(1), string).trim 'fathercode
title = "[" & id & "] " & directcast(r.item(2), string).trim 'title
'查詢與父節點id相同的節點
fn = findnode(fatherid)
if fn is
nothing
then
'沒找到對應id的節點
else
node = new treenode
with node
.tag = id
.text = title
endwith
fn.nodes.add(node)
endifnext
bootnode.expand()
endfunction
'在指定的節點下查詢id相符節點
private
function findnode(byval n as treenode, byval id as
string) as treenode
dim ns as treenodecollection
dim node as treenode
dim flag as
boolean
dim strtext as
string
dim returnnode as treenode
flag = false
if n.tag = id then
return n
else
'如果路徑根本不相同則返回
if (id.length < directcast(n.tag, string).length) orelse ((n.tag <> "0") andalso (id.substring(0, n.tag.length) <> n.tag)) then
return
nothing
endif
ns = n.nodes
foreach node in ns
returnnode = findnode(node, id)
if returnnode is
nothing
then
'do nothing
else
flag = true
exit
forend
ifnext
if flag = true
then
return returnnode
endifreturn
nothing
endfunction
以上的**利用將當前字節點的id值儲存於node的tag中,然後從根目錄開始進行遍歷進行查詢。執行後發現其效率極為低下。但是網上的**大多如此。
有沒有其它方法可以增快其執行速度呢。
神說:「演算法要麼以空間換時間,要麼以時間換空間」。
在某個時候我於是頓悟了。我發現我可以.net提供的hashtable,這可是個好東東。
那麼如何來使用它呢?
二、快速的方法
首先我們需要增加乙個定義
private
fasthashtable as hashtable
然後需要將buildentree來做點小調整
private
function buildentree(byval ds as dataset)
dim rs as datarowcollection
dim r as datarow
dim id as
string
dim fatherid as
string
dim title as
string
dim fn as treenode
dim node as treenode
rs = ds.tables(0).rows
bootnode = new treenode
bootnode.text = "[0] 所有單位"
bootnode.tag = "0" '根目錄
treeen.nodes.add(bootnode)
fasthashtable.add("0", bootnode)
foreach r in rs
id = directcast(r.item(0), string).trim 'code
fatherid = directcast(r.item(1), string).trim 'fathercode
title = "[" & id & "] " & directcast(r.item(2), string).trim 'title
'查詢與父節點id相同的節點
fn = findnode(fatherid)
if fn is
nothing
then
'沒找到對應id的節點
else
node = new treenode
with node
.tag = id
.text = title
endwith
fn.nodes.add(node)
fasthashtable.add(id, node)
endif
next
bootnode.expand()
endfunction
從以上**看出,我只是增加了兩處黑體的**,它的作用就是將新增的節點不僅存到treeview中,還要存到fashhashtable中,其關鍵字是id值,只要沒有重複的id,那麼就可以使用。
接下來我們還需要對findnode進行改變。
'新版的快速查詢
private
function findnode(byval id as
string) as treenode
return fasthashtable.item(id)
endfunction
好了,現在還有乙個工作要做,就是要將fasthashtable進行初使化,盡量將其初使化成與你將要構建的資料節點數相同,我這個系統中是3000個節點,因此我增加了以下一句:
fasthashtable = new hashtable(3000) '數量
現在,這個樹已經很快了。
構建高效的MySQL分頁
mysql分頁的主要目的就是提公升效率,今天我們將簡單介紹如何構建高效mysql分頁。首先看一下分頁的基本原理 limit 10000,20的意思掃瞄滿足條件的10020行,扔掉前面的10000行,返回最後的20行,問題就在這裡,如果是limit 100000,100,需要掃瞄100100行,在乙個...
構建高效運作的PMO
構建高效運作的pmo 近年來,越來越多的企業設立了專案管理辦公室 project management office,pmo 不同企業對專案管理辦公室的職能有不同的理解,因而專案管理辦公室在不同的企業起到的作用也各不相同。不管如何定位pmo,pmo的設立對大多數專案很多的企業而言是具有積極意義的。企...
關於treeview手動新增的方法
1.首先判斷有沒有父節點,有父節點的,new乙個父節點然後增加 沒有父節點就選當前的節點 treeview2.nodes.add newchildnode 2.父節點的判斷可以用level來判斷 3.下面的程式其實是增加第二個階梯 level 1 當你右擊第乙個階梯時 level 0,父節點 那麼程...