0
i need to view a tree from a single table in c#.
the table contain four fields which are group_id,group_name,is_sub_group,main_group_id. I need to display the main group as root node and the groups coming under the main group as child node
7 Antworten
+ 3
And this question is regarding the SQL to retrieve the data or how to populate the data into the TreeView? if you have the code in progress you can link it here, at least part of it, so others can help you with it :)
+ 3
Can you provide details of the table fields? like what type of data they contain, and your current code for adding new node?
My idea would be to assign the group_id as node key (identifier) which later can be used to reference to the node, when we need to add a child node, we refer to the parent node by using main_group_id, of course we only do that if is_sub_group evaluates to true, otherwise we'll just add new node at top hierarchy.
+ 2
Alright mate, good to know that, btw, you don't need to include connection string details here, it's your private information, I know it's local, but still.
I see you use PostgreSQL here, I've never experienced it, in your opinion, how is it? compared with MySQL for example, is there anything special in PostgreSQL? would be nice to hear from a fellow coder though.
And one more thing, why did you used two datasets for this? is it not possible with just one? will you kindly share the reason for that?
+ 1
this was just a test to check the treeview and i am also new to the postgresql.
its same as mysql and the advantage is it's free
+ 1
Okay mate, good sharing, best of luck on that project :D
0
i need to populate the treeview.
0
thanks i got it.
String strConn = @"Server = 192.168.27.27; Port = 5432; User Id = postgres; Password = postgres; Database = ERP_wrk";
NpgsqlConnection conn = new NpgsqlConnection(strConn);
NpgsqlDataAdapter da = new NpgsqlDataAdapter("Select * from item_group where is_sub_group=false order by sl_no", conn);
DataSet ds = new DataSet();
da.Fill(ds, "item_group");
foreach (DataRow dr in ds.Tables["item_group"].Rows)
{
String tn=dr["group_id"].ToString();
String name = dr["group_name"].ToString();
//foreach (DataRow drChild in dr.GetChildRows("item_group"))
//{
// tn.Nodes.Add(drChild["group_id"].ToString());
//}
treeView1.Nodes.Add(tn,name);
}
//select * from item_group where is_sub_group;
NpgsqlDataAdapter da2 = new NpgsqlDataAdapter("Select * from item_group where is_sub_group=true order by sl_no", conn);
DataSet ds2 = new DataSet();
da2.Fill(ds2, "item_group");
foreach (DataRow dr2 in ds2.Tables["item_group"].Rows)
{
String pid = dr2["main_group_id"].ToString();
String tn = dr2["group_id"].ToString();
String name = dr2["group_name"].ToString();
var result = treeView1.Nodes.Find(pid, true).FirstOrDefault();
if (result != null)
{
result.Nodes.Add(tn, name);
}