Advertisement
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: |
Imports System
Imports System.Data
Imports System.Web.UI
Imports System.Collections
'***********************************************************************
' Written By Ralph Varjabedian
' You may use this code freely and copy this code provided that
' You do not remove this copyright notice.
' April 2008
' http://www.varjabedian.net/archive/2008/04/22/binding-asp.net-treeview-to-a-dataset-or-an-objectdatasource.aspx
'***********************************************************************
Namespace TreeViewBindingTest
''' <summary>
''' A class that translates a DataSet into IHierarchicalDataSource that can be used to bind Hierarchical data to a TreeView
''' </summary>
Public Class HierarchicalDataSet
Implements IHierarchicalDataSource
Private dataSet As DataSet
Private idColumnName As String
Private parentIdColumnName As String
''' <summary>
''' The constructor of the class
''' </summary>
''' <param name="dataSet">The dataset that contains the data</param>
''' <param name="idColumnName">The Primary key column name</param>
''' <param name="parentidColumnName">The Parent Primary key column name that identifies the Parent-Child relationship</param>
Public Sub New(ByVal dataSet As DataSet, ByVal idColumnName As String, ByVal parentIdColumnName As String)
Me.dataSet = dataSet
Me.idColumnName = idColumnName
Me.parentIdColumnName = parentIdColumnName
End Sub
Public Event DataSourceChanged(ByVal sender As Object, ByVal e As EventArgs)
' never used here
Public Function GetHierarchicalView(ByVal viewPath As String) As HierarchicalDataSourceView
Return New DataSourceView(Me, viewPath)
End Function
#Region "supporting methods"
Private Function GetParentRow(ByVal row As DataRowView) As DataRowView
dataSet.Tables(0).DefaultView.RowFilter = [String].Format("{0} = {1}", idColumnName, row(parentIdColumnName).ToString())
Dim parentRow As DataRowView = dataSet.Tables(0).DefaultView(0)
dataSet.Tables(0).DefaultView.RowFilter = ""
Return parentRow
End Function
Private Function GetChildrenViewPath(ByVal viewPath As String, ByVal row As DataRowView) As String
Return viewPath + "\" + row(idColumnName).ToString()
End Function
Private Function HasChildren(ByVal row As DataRowView) As Boolean
dataSet.Tables(0).DefaultView.RowFilter = [String].Format("{0} = {1}", parentIdColumnName, row(idColumnName))
Dim hasChildren As Boolean = dataSet.Tables(0).DefaultView.Count > 0
dataSet.Tables(0).DefaultView.RowFilter = ""
Return hasChildren
End Function
Private Function GetParentViewPath(ByVal viewPath As String) As String
Return viewPath.Substring(0, viewPath.LastIndexOf("\"))
End Function
#End Region
#Region "private classes that implement further interfaces"
Private Class DataSourceView
Inherits HierarchicalDataSourceView
Private hDataSet As HierarchicalDataSet
Private viewPath As String
Public Sub New(ByVal hDataSet As HierarchicalDataSet, ByVal viewPath As String)
Me.hDataSet = hDataSet
Me.viewPath = viewPath
End Sub
Public Overloads Overrides Function [Select]() As IHierarchicalEnumerable
Return New HierarchicalEnumerable(hDataSet, viewPath)
End Function
End Class
Private Class HierarchicalEnumerable
Implements IHierarchicalEnumerable
Private hDataSet As HierarchicalDataSet
Private viewPath As String
Public Sub New(ByVal hDataSet As HierarchicalDataSet, ByVal viewPath As String)
Me.hDataSet = hDataSet
Me.viewPath = viewPath
End Sub
Public Function GetHierarchyData(ByVal enumeratedItem As Object) As IHierarchyData
Dim row As DataRowView = DirectCast(enumeratedItem, DataRowView)
Return New HierarchyData(hDataSet, viewPath, row)
End Function
Public Function GetEnumerator() As IEnumerator
If viewPath = "" Then
hDataSet.dataSet.Tables(0).DefaultView.RowFilter = [String].Format("{0} is null", hDataSet.parentIdColumnName)
Else
Dim lastID As String = viewPath.Substring(viewPath.LastIndexOf("\") + 1)
hDataSet.dataSet.Tables(0).DefaultView.RowFilter = [String].Format("{0} = {1}", hDataSet.parentIdColumnName, lastID)
End If
Dim i As IEnumerator = hDataSet.dataSet.Tables(0).DefaultView.GetEnumerator()
hDataSet.dataSet.Tables(0).DefaultView.RowFilter = ""
Return i
End Function
End Class
Private Class HierarchyData
Implements IHierarchyData
Private hDataSet As HierarchicalDataSet
Private row As DataRowView
Private viewPath As String
Public Sub New(ByVal hDataSet As HierarchicalDataSet, ByVal viewPath As String, ByVal row As DataRowView)
Me.hDataSet = hDataSet
Me.viewPath = viewPath
Me.row = row
End Sub
Public Function GetChildren() As IHierarchicalEnumerable
Return New HierarchicalEnumerable(hDataSet, hDataSet.GetChildrenViewPath(viewPath, row))
End Function
Public Function GetParent() As IHierarchyData
Return New HierarchyData(hDataSet, hDataSet.GetParentViewPath(viewPath), hDataSet.GetParentRow(row))
End Function
Public ReadOnly Property HasChildren() As Boolean
Get
Return hDataSet.HasChildren(row)
End Get
End Property
Public ReadOnly Property Item() As Object
Get
Return row
End Get
End Property
Public ReadOnly Property Path() As String
Get
Return viewPath
End Get
End Property
Public ReadOnly Property Type() As String
Get
Return GetType(DataRowView).ToString()
End Get
End Property
End Class
#End Region
End Class
End Namespace
|
|
[x]
The Solution Rating System
|
||
|
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
|
Loading Advertisement... |