下面是我用来从数据库将数据加载到treeview中的代码!但我觉得它有点滞后,而检索......是否有任何方法来优化它?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
If Not IsPostBack Then
PopulateRootLevel("0", "ALL")
End If
Catch ex As Exception
MSGMgr.errHandlerSys(ex.Message, lblMsg)
End Try
End Sub
Private Sub PopulateRootLevel(ByVal LvlCD As String, ByVal scheme As String)
Dim objConn As New SqlConnection(DBMgr.asyncADOCnnStr("SQL"))
Dim objCommand As SqlCommand
If scheme = "ALL" Then
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, GroupNo, Descriptions Title, (SELECT DISTINCT COUNT(*) FROM i_Menu_Access WHERE GroupNo = prt.GroupNo and SubItemNo1='0' AND LevelCD='0' AND ItemNo <> '0') SubItemCnt FROM i_Menu_Access prt WHERE ItemNo = '0' AND LevelCD = '" & LvlCD & "' ORDER BY GroupNo", objConn)
Else
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, GroupNo, Descriptions Title, (SELECT DISTINCT COUNT(*) FROM i_Menu_Access WHERE GroupNo = prt.GroupNo and SubItemNo1='0' AND LevelCD='0' AND ItemNo <> '0') SubItemCnt FROM i_Menu_Access prt WHERE ItemNo = '0' AND LevelCD = '" & LvlCD & "' AND Level3 = @Scheme ORDER BY GroupNo", objConn)
objCommand.Parameters.Add("@Scheme", SqlDbType.VarChar).Value = scheme
End If
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)
For Each dr As DataRow In dt.Rows
Dim child As New TreeNode()
child.Text = dr("Title").ToString().Trim()
child.Value = dr("ID").ToString().Trim()
Dim GrpNo As String = dr("GroupNo").ToString().Trim()
child.ToolTip = "Click to get Child"
child.SelectAction = TreeNodeSelectAction.SelectExpand
child.CollapseAll()
tvMenuTable.Nodes.Add(child)
PopulateSubLevel(GrpNo, LvlCD, child, scheme)
Next
End Sub
Private Sub PopulateSubLevel(ByVal parentid As Integer, ByVal LvlCD As String, ByVal parentNode As TreeNode, ByVal scheme As String)
Dim objConn As New SqlConnection(DBMgr.asyncADOCnnStr("SQL"))
Dim objCommand As SqlCommand
If scheme = "ALL" Then
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, cast(groupno as varchar)+'|'+cast(itemno as varchar)+'|'+cast(subitemno1 as varchar) ID2, Descriptions Title, (SELECT DISTINCT COUNT(*) FROM i_Menu_Access WHERE GroupNo = prt.GroupNo AND ItemNo = prt.ItemNo AND SubItemNo1 <> '0' AND SubItemNo2 = '0' AND LevelCD = '0') SubItemCnt FROM i_Menu_Access prt WHERE LevelCD = @LvlCD AND GroupNo = @parentID AND ItemNo <> '0' AND SubItemNo1 = '0' ORDER BY ItemNo", objConn)
objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
Else
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, cast(groupno as varchar)+'|'+cast(itemno as varchar)+'|'+cast(subitemno1 as varchar) ID2, Descriptions Title, (SELECT DISTINCT COUNT(*) FROM i_Menu_Access WHERE GroupNo = prt.GroupNo AND ItemNo = prt.ItemNo AND SubItemNo1 <> '0' AND SubItemNo2 = '0' AND LevelCD = '0') SubItemCnt FROM i_Menu_Access prt WHERE LevelCD = @LvlCD AND GroupNo = @parentID AND ItemNo <> '0' AND SubItemNo1 = '0' AND Level3 = @Scheme ORDER BY ItemNo", objConn)
objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
objCommand.Parameters.Add("@Scheme", SqlDbType.VarChar).Value = scheme
End If
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)
For Each dr As DataRow In dt.Rows
Dim child As New TreeNode()
child.Text = dr("Title").ToString().Trim()
child.Value = dr("ID").ToString().Trim()
Dim ID() As String
ID = dr("ID2").ToString().Trim().Split("|")
child.ToolTip = "Click to get Child"
child.SelectAction = TreeNodeSelectAction.SelectExpand
child.CollapseAll()
parentNode.ChildNodes.Add(child)
PopulateThirdLevel(ID(0), ID(1), LvlCD, child, scheme)
Next
End Sub
Private Sub PopulateThirdLevel(ByVal lvlOneID As Integer, ByVal lvlTwoID As Integer, ByVal LvlCD As String, ByVal parentNode As TreeNode, ByVal scheme As String)
Dim objConn As New SqlConnection(DBMgr.asyncADOCnnStr("SQL"))
Dim objCommand As SqlCommand
If scheme = "ALL" Then
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, SubItemNo1, cast(groupno as varchar)+'|'+cast(itemno as varchar)+'|'+cast(subitemno1 as varchar) ID2, Descriptions Title FROM i_Menu_Access WHERE LevelCD = @LvlCD AND GroupNo = @lvlOneID AND ItemNo = @lvlTwoID AND ItemNo <> '0' AND SubItemNo1 <> '0' AND SubItemNo2 = '0' ORDER BY SubItemNo1", objConn)
objCommand.Parameters.Add("@lvlOneID", SqlDbType.Int).Value = lvlOneID
objCommand.Parameters.Add("@lvlTwoID", SqlDbType.Int).Value = lvlTwoID
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
Else
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, SubItemNo1, cast(groupno as varchar)+'|'+cast(itemno as varchar)+'|'+cast(subitemno1 as varchar) ID2, Descriptions Title FROM i_Menu_Access WHERE LevelCD = @LvlCD AND GroupNo = @lvlOneID AND ItemNo = @lvlTwoID AND ItemNo <> '0' AND SubItemNo1 <> '0' AND SubItemNo2 = '0' AND Level3 = @Scheme ORDER BY SubItemNo1", objConn)
objCommand.Parameters.Add("@lvlOneID", SqlDbType.Int).Value = lvlOneID
objCommand.Parameters.Add("@lvlTwoID", SqlDbType.Int).Value = lvlTwoID
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
objCommand.Parameters.Add("@Scheme", SqlDbType.VarChar).Value = scheme
End If
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)
For Each dr As DataRow In dt.Rows
Dim child As New TreeNode()
child.Text = dr("Title").ToString().Trim()
child.Value = dr("ID").ToString().Trim()
Dim ID() As String
ID = dr("ID2").ToString().Trim().Split("|")
child.SelectAction = TreeNodeSelectAction.SelectExpand
child.CollapseAll()
parentNode.ChildNodes.Add(child)
Populate4thLevel(ID(0), ID(1), ID(2), LvlCD, child, scheme)
Next
End Sub
Private Sub Populate4thLevel(ByVal lvlOneID As Integer, ByVal lvlTwoID As Integer, ByVal lvl3rdID As Integer, ByVal LvlCD As String, ByVal parentNode As TreeNode, ByVal scheme As String)
Dim objConn As New SqlConnection(DBMgr.asyncADOCnnStr("SQL"))
Dim objCommand As SqlCommand
If scheme = "ALL" Then
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, SubItemNo2, Descriptions Title FROM i_Menu_Access WHERE LevelCD = @LvlCD AND GroupNo = @lvlOneID AND ItemNo = @lvlTwoID AND SubItemNo1 = @lvl3rdID AND SubItemNo2 <> '0' ORDER BY SubItemNo2", objConn)
objCommand.Parameters.Add("@lvlOneID", SqlDbType.Int).Value = lvlOneID
objCommand.Parameters.Add("@lvlTwoID", SqlDbType.Int).Value = lvlTwoID
objCommand.Parameters.Add("@lvl3rdID", SqlDbType.Int).Value = lvl3rdID
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
Else
objCommand = New SqlCommand("SELECT DISTINCT LevelCD, Level1 IsChecked, LevelCD+Level2+'|'+Level1+'|'+CASE WHEN Level3 IS NULL THEN '' ELSE Level3 END ID, Level3 Scheme, ItemNo, SubItemNo2, Descriptions Title FROM i_Menu_Access WHERE LevelCD = @LvlCD AND GroupNo = @lvlOneID AND ItemNo = @lvlTwoID AND SubItemNo1 = @lvl3rdID AND SubItemNo2 <> '0' AND Level3 = @Scheme ORDER BY SubItemNo2", objConn)
objCommand.Parameters.Add("@lvlOneID", SqlDbType.Int).Value = lvlOneID
objCommand.Parameters.Add("@lvlTwoID", SqlDbType.Int).Value = lvlTwoID
objCommand.Parameters.Add("@lvl3rdID", SqlDbType.Int).Value = lvl3rdID
objCommand.Parameters.Add("@LvlCD", SqlDbType.VarChar).Value = LvlCD
objCommand.Parameters.Add("@Scheme", SqlDbType.VarChar).Value = scheme
End If
Dim da As New SqlDataAdapter(objCommand)
Dim dt As New DataTable()
da.Fill(dt)
For Each dr As DataRow In dt.Rows
Dim child As New TreeNode()
child.Text = dr("Title").ToString().Trim()
child.Value = dr("ID").ToString().Trim()
child.SelectAction = TreeNodeSelectAction.SelectExpand
child.CollapseAll()
parentNode.ChildNodes.Add(child)
Next
End Sub
答案 0 :(得分:0)
好吧,如果您正确设计了数据库,那么您只需要一个功能即可获得&amp;填充任何级别的节点。例如,假设考虑一个示例数据库表'Items',例如
ItemId int NOT NULL PK
ItemText varchar(200)
ParentId int NULL FK
请注意,我们已将ParentId
上的FK与相同的Items
表放在一起,并且可以为NULL,表示根级别项。现在,您可以使用
SELECT ItemId, ItemText, ParentId FROM Items WHERE ParentId = @ParentId
如果要检索根级别项目,请将@ParentId
作为NULL传递 - 展开任何节点并获取子项,将ItemId
作为ParentId
传递给上述查询。< / p>
答案 1 :(得分:0)
您可以在数据集中添加数据表,并在数据集中设置父子关系。
这是完整,干净和优化的代码。我在必要时写了评论
DataTable dtbl1=new DataTable();//parent datatable
DataTable dtbl2=new DataTable();//child datatable
DataSet ds = new DataSet();
ds.Tables.Add(dtbl1);
ds.Tables.Add(dtbl2);
ds.Relations.Add("Children", dtbl1.Columns["dtb1ID"], dtbl2.Columns["dtbl2ID"]);//define parent child relation in dataset
if (ds.Tables[0].Rows.Count > 0)
{
trv.Nodes.Clear();
Int32 count = 0;
foreach(DataRow masterRow in ds.Tables[0].Rows)
{
TreeNode masterNode = new TreeNode((String)masterRow["dtbl1ColumnYouWantToDisplay"], Convert.ToString(masterRow["dtbl1ID"]));
trv.Nodes.Add(masterNode);
foreach (DataRow childRow in masterRow.GetChildRows("Children"))
{
TreeNode childNode = new TreeNode((String)childRow["dtbl2ColumnYouWantToDisplay"], Convert.ToString(childRow["dtb2ID"]));
masterNode.ChildNodes.Add(childNode);
count++;
}
}
trv.ExpandAll();
}