بسم الله الرحمن الرحيم السلام عليكم ورحمة الله وبركاته اريد ربط ال treeview بجدول واحد هل ممكن تساعوني ؟ وهي بالمرفقات حيث العمود account_id هو رقم الحساب و acccount هو اسم الحساب و type هو رقم الحساب الاب(الرئيسي).استخدم vb2005 وشكراً مقدماً
ربط ال treeview بقاعدة البيانات
أخى جرب هذا الكود
static string queryString = "select * from accounts";
OleDbConnection cn = new OleDbConnection(connectionString);
DataSet dsTreeView = new DataSet();
// Populate public DataSet
private DataSet PopulateDataTable()
{
OleDbCommand cmd = new OleDbCommand(queryString, cn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
try
{
cn.Open();
if (!dsTreeView.Tables.Contains("tree"))
dsTreeView.Tables.Add("tree");
da.Fill(dsTreeView, "tree");
}
catch (OleDbException oex)
{
throw new Exception(oex.Message);
}
finally
{
cn.Close();
}
return dsTreeView;
}
// The level of tree
int intLevel = 0;
private TreeNode BuildNode(string Text)
{
return new TreeNode(Text);
}
// Recursive function to populate Treeview node
public void FillChildren(TreeNode _Parent, int _ParentID)
{
intLevel++;
DataView Data = new DataView(dsTreeView.Tables["tree"]);
Data.RowFilter = "Type = " + _ParentID;
TreeNode[] myTreeNodeArray = new TreeNode[5];
// Define the image index in the lowest level
if (Data.Count == 0)
{
_Parent.ImageIndex = 2;
_Parent.SelectedImageIndex = 2;
}
foreach (DataRowView foo in Data)
{
TreeNode t = BuildNode((string)foo["account"]);
t.Tag = intLevel;
FillChildren(t, int.Parse(foo["account_id"].ToString()));
_Parent.Nodes.Add(t);
System.Diagnostics.Trace.WriteLine("Added node at level " + intLevel.ToString());
}
intLevel--;
}
private void LoadMenuTreeView()
{
PopulateDataTable();
DataView Data = new DataView(dsTreeView.Tables["tree"]);
Data.RowFilter = "Type is null ";
foreach (DataRowView foo in Data)
{
TreeNode t = BuildNode((string)foo["account"]);
t.Tag = 0;
FillChildren(t, int.Parse(foo["account_id"].ToString()));
this.treeViewAccounts.Nodes.Add(t);
}
}وممّا زادنـــي شرفــاً وتيهــاً ** وكدت بأخمصي أطأ الثريّا
دخولي تحت قولك يا عبادي ** وأن صيّرت أحمد لي نبيّـاً
شكرا اخي الكريم عمرو الصعيدي ابرمج ب vb2005 ممكن تحوله
تم تعديل هذه المشاركة بواسطة vbdotnet2005 في 20 أبريل 2010 في 18:21
الله في عون العبد مادام العبد في عون اخيه
أخى تفضل الكود ب VB.NET
مع ملاحظة تعديل جملة الأتصال
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\db.mdb;Persist Security Info=False"
Dim queryString As String = "select * from accounts"
Private cn As New OleDbConnection(connectionString)
Private dsTreeView As New DataSet()
' Populate public DataSet
Private Function PopulateDataTable() As DataSet
Dim cmd As New OleDbCommand(queryString, cn)
Dim da As New OleDbDataAdapter(cmd)
Try
cn.Open()
If Not dsTreeView.Tables.Contains("tree") Then
dsTreeView.Tables.Add("tree")
End If
da.Fill(dsTreeView, "tree")
Catch oex As OleDbException
Throw New Exception(oex.Message)
Finally
cn.Close()
End Try
Return dsTreeView
End Function
' The level of tree
Private intLevel As Integer = 0
Private Function BuildNode(ByVal Text As String) As TreeNode
Return New TreeNode(Text)
End Function
' Recursive function to populate Treeview node
Public Sub FillChildren(ByVal _Parent As TreeNode, ByVal _ParentID As Integer)
intLevel += 1
Dim Data As New DataView(dsTreeView.Tables("tree"))
Data.RowFilter = "Type = " & _ParentID
Dim myTreeNodeArray As TreeNode() = New TreeNode(4) {}
' Define the image index in the lowest level
If Data.Count = 0 Then
_Parent.ImageIndex = 2
_Parent.SelectedImageIndex = 2
End If
For Each foo As DataRowView In Data
Dim t As TreeNode = BuildNode(DirectCast(foo("account"), String))
t.Tag = intLevel
FillChildren(t, Integer.Parse(foo("account_id").ToString()))
_Parent.Nodes.Add(t)
System.Diagnostics.Trace.WriteLine("Added node at level " & intLevel.ToString())
Next
intLevel -= 1
End Sub
Private Sub LoadMenuTreeView()
PopulateDataTable()
Dim Data As New DataView(dsTreeView.Tables("tree"))
Data.RowFilter = "Type is null "
For Each foo As DataRowView In Data
Dim t As TreeNode = BuildNode(DirectCast(foo("account"), String))
t.Tag = 0
FillChildren(t, Integer.Parse(foo("account_id").ToString()))
Me.TreeView1.Nodes.Add(t)
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadMenuTreeView()
End Subوممّا زادنـــي شرفــاً وتيهــاً ** وكدت بأخمصي أطأ الثريّا
دخولي تحت قولك يا عبادي ** وأن صيّرت أحمد لي نبيّـاً
الله في عون العبد مادام العبد في عون اخيه
أخى فى مشكله عندك لأنه غير قادر على قراءة جدول accounts
أخى تأكد من اسم الجدول فى قاعدة البيانات
تم تعديل هذه المشاركة بواسطة عمرو الصعيدي في 25 أبريل 2010 في 00:50
وممّا زادنـــي شرفــاً وتيهــاً ** وكدت بأخمصي أطأ الثريّا
دخولي تحت قولك يا عبادي ** وأن صيّرت أحمد لي نبيّـاً