Hello,
I am recently creating a form that has a tree view in it. I want to populate the treeview through database. Help Required.
Thanx in advance.
Regards!
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
kiran kumarPosted Feb 19, 2010, 8:41 PM
conn.Open();
SqlCommand cmd = new SqlCommand(getOnline,conn);
SqlDataReader reader = null;
this.trv_ShowUsers.Nodes.Add(Online);
reader = cmd.ExecuteReader();
while (reader.Read())
{
Online.Text = "Online Users";
Online.Nodes.Add(reader[0].ToString());
}
conn.Close();
mark the answer if it helps you
Farrukh JaveidPosted Feb 20, 2010, 1:59 AM
Thanx for the help. This thing is started to work now :). You're the best!
Regards!
Farrukh JaveidPosted Feb 19, 2010, 12:32 PM
Thanx for the response. My query for the retrieval of record is
string getOnline = "Select Friend from FriendList";
and it gets all the fields in the 'Friend' column and put them in the tree view under the same Root/Node i.e. Online.
Any information about the exception?
Thanx for the code and the response.
Regards!
kiran kumarPosted Feb 19, 2010, 11:59 AM
MARK THE ANSWER IF IT HELPED YOU
Farrukh JaveidPosted Feb 19, 2010, 10:51 AM
conn.Open();
SqlCommand cmd = new SqlCommand(getOnline,conn);
SqlDataReader reader = null;
reader = cmd.ExecuteReader();
while (reader.Read())
{
this.trv_ShowUsers.Nodes.Add(Online);
Online.Text = "Online Users";
Online.Nodes.Add(reader[0].ToString());
}
conn.Close();
This is the code but its giving the exception of "Cannot add or insert the item 'Online Users' in more than one place. You must first remove it from its current location or clone it.".
kiran kumarPosted Feb 18, 2010, 10:31 PM
suppose this is my database table
rollno name class
1231 kiran 10
1232 kumar 9
1233 kkk 8
CHECK THE CODE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace treeview
{
public partial class Form1 : Form
{
TreeView tr = new TreeView();
TreeNode[] student = new TreeNode[10];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.tr.Location = new Point(60, 35);
this.tr.Size = new Size(341, 246);
SqlConnection con = new SqlConnection("Data Source=KIRAN\\SQLEXPRESS;Initial Catalog=kiran;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select * from tree",con);
SqlDataReader dr = cmd.ExecuteReader();
int i=0;
while (dr.Read())
{
i++;
student[i] = new TreeNode();
student[i].Text = "student"+i+"";
this.tr.Nodes.Add(student[i]);
this.student[i].Nodes.Add(dr["rollno"].ToString());
this.student[i].Nodes.Add(dr["name"].ToString());
this.student[i].Nodes.Add(dr["class"].ToString());
}
con.Close();
this.Controls.Add(tr);
}
}
}
MARK THE ANSWER IF IT HELPED YOU