c#中treeview控件与数据库的绑定

2024年11月16日 12:06
有3个网友回答
网友(1):

Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.Web.UI.WebControls
Imports System.Web
Imports System.Drawing
Imports System
Imports System.Xml

Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web 窗体设计器生成的代码 "

'该调用是 Web 窗体设计器所必需的。
Private Sub InitializeComponent()

End Sub
Protected WithEvents TreeView1 As Microsoft.Web.UI.WebControls.TreeView

'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Dim strConn As String = "server=localhost;uid=sa;pwd=123456;database=Northwind;"
Dim objConn As New SqlConnection(strConn)
Dim objDS As New DataSet
Dim daSuppliers As New SqlDataAdapter("SELECT CompanyName,SupplierID FROM Suppliers", objConn)
Dim daProducts As New SqlDataAdapter("SELECT ProductName, ProductID, SupplierID FROM Products", objConn)
daSuppliers.Fill(objDS, "dtSuppliers")
daProducts.Fill(objDS, "dtProducts")
objConn.Close()
objDS.Relations.Add("SuppToProd", _
objDS.Tables("dtSuppliers").Columns("SupplierID"), _
objDS.Tables("dtProducts").Columns("SupplierID"))
Dim nodeSupp, nodeProd As TreeNode
Dim rowSupp, rowProd As DataRow
For Each rowSupp In objDS.Tables("dtSuppliers").Rows
nodeSupp = New TreeNode
nodeSupp.Text = rowSupp("CompanyName")
nodeSupp.ID = rowSupp("SupplierID")
TreeView1.Nodes.Add(nodeSupp)
For Each rowProd In rowSupp.GetChildRows("SuppToProd")
nodeProd = New TreeNode
nodeProd.Text = rowProd("ProductName")
nodeProd.ID = rowProd("ProductID")
nodeSupp.Nodes.Add(nodeProd)
Next
Next
'clean(up)
objDS.Dispose()
daSuppliers.Dispose()
daProducts.Dispose()
objConn.Close()
objConn.Dispose()
End Sub
End Class

网友(2):

Imports System.Data
Imports System.Data.SqlClient
Imports Microsoft.Web.UI.WebControls
Imports System.Web
Imports System.Drawing
Imports System
Imports System.Xml

Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web 窗体设计器生成的代码 "

'该调用是 Web 窗体设计器所必需的。
Private Sub InitializeComponent()

End Sub
Protected WithEvents TreeView1 As Microsoft.Web.UI.WebControls.TreeView

'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Dim strConn As String = "server=localhost;uid=sa;pwd=123456;database=Northwind;"
Dim objConn As New SqlConnection(strConn)
Dim objDS As New DataSet
Dim daSuppliers As New SqlDataAdapter("SELECT CompanyName,SupplierID FROM Suppliers", objConn)
Dim daProducts As New SqlDataAdapter("SELECT ProductName, ProductID, SupplierID FROM Products", objConn)
daSuppliers.Fill(objDS, "dtSuppliers")
daProducts.Fill(objDS, "dtProducts")
objConn.Close()
objDS.Relations.Add("SuppToProd", _
objDS.Tables("dtSuppliers").Columns("SupplierID"), _
objDS.Tables("dtProducts").Columns("SupplierID"))
Dim nodeSupp, nodeProd As TreeNode
Dim rowSupp, rowProd As DataRow
For Each rowSupp In objDS.Tables("dtSuppliers").Rows
nodeSupp = New TreeNode
nodeSupp.Text = rowSupp("CompanyName")
nodeSupp.ID = rowSupp("SupplierID")
TreeView1.Nodes.Add(nodeSupp)
For Each rowProd In rowSupp.GetChildRows("SuppToProd")
nodeProd = New TreeNode
nodeProd.Text = rowProd("ProductName")
nodeProd.ID = rowProd("ProductID")
nodeSupp.Nodes.Add(nodeProd)
Next
Next
'clean(up)
objDS.Dispose()
daSuppliers.Dispose()
daProducts.Dispose()
objConn.Close()
objConn.Dispose()
End Sub
End Class

网友(3):

qqq