How to populate a treeview from a dataset
-
mercoledì 7 marzo 2012 15:44
Hi!
I am very new to C#. I need to create a Windows form that will read a SQL table and populate a treeview. I can connect to the <acronym style="border-style:initial;border-color:initial;cursor:help;">DB</acronym>, create the dataadapter, populate a data set. The problem is how to use the dataset to populate a treeview.
I have looked at a few examples but none use a dataset, or the data structure was different and I could not modify to work with my data or the examples were more than I needed and too complex for a beginner.
I have a sql server containing the fields: ID, Date, Name
01-Mar-2012|----A|----B|----C02-Mar-2012|----D|----E|----FI want to populate the data like above.
I would really appreciate the help.
Thank you.Apurba Doley
Tutte le risposte
-
venerdì 9 marzo 2012 03:38Moderatore
Hi Apurba,
Iterate through the DataTable and add Node to the TreeView, seeDataSet ds = new DataSet(); DataTable dt = new DataTable(); private void Form1_Load(object sender, EventArgs e) { dt.TableName = "YourTableName"; dt.Columns.Add("ID"); dt.Columns.Add("Date",typeof(DateTime)); dt.Columns.Add("Name"); dt.Rows.Add("1", DateTime.Parse("01-Mar-2012"),"A"); dt.Rows.Add("2", DateTime.Parse("01-Mar-2012"), "B"); dt.Rows.Add("3", DateTime.Parse("01-Mar-2012"), "C"); dt.Rows.Add("4", DateTime.Parse("02-Mar-2012"), "D"); dt.Rows.Add("5", DateTime.Parse("02-Mar-2012"), "E"); dt.Rows.Add("6", DateTime.Parse("02-Mar-2012"), "F"); ds.Tables.Add(dt); foreach (DataRow dr in ds.Tables["YourTableName"].Rows) { TreeNode tn; TreeNode tn2 = new TreeNode(dr["Name"].ToString()); string drDate = ((DateTime)dr["Date"]).ToString("dd-MMM-yyyy"); if (treeView1.Nodes.IndexOfKey(drDate) == -1) { tn = new TreeNode(drDate); tn.Name = drDate; tn.Nodes.Add(tn2); treeView1.Nodes.Add(tn); } else { tn = treeView1.Nodes[treeView1.Nodes.IndexOfKey(drDate)]; tn.Nodes.Add(tn2); } } }
If you still have any doubt about this issue, please let me know.
Best Regards,Bob Wu [MSFT]
MSDN Community Support | Feedback to us
-
venerdì 9 marzo 2012 07:19
Hi Bob! Thanks for your code. It helped me a lot. I need to populate dynamically from the database. For you kind information i have one table consisting fields: ID, Date, Name.
Example:
ID Date Name
1 01/03/2012 A
2 01/03/2012 B
3 01/03/2012 C
4 01/03/2012 D
5 02/03/2012 E
6 02/03/2012 F
7 02/03/2012 G
8 02/03/2012 H
So i need to display all names for a particular date accordingly. How do i do please suggest. Thank you.
Apurba Doley
-
venerdì 9 marzo 2012 07:37Moderatore
It’s similar just compare the particular date and the data in current row.
treeView1.Nodes.Clear(); string particularDate = "02-Mar-2012"; foreach (DataRow dr in ds.Tables["YourTableName"].Rows) { TreeNode tn; TreeNode tn2 = new TreeNode(dr["Name"].ToString()); //string drDate = ((DateTime)dr["Date"]).ToString("dd-MMM-yyyy"); string drDate = particularDate; if(!string.Equals(drDate,((DateTime)dr["Date"]).ToString("dd-MMM-yyyy"),StringComparison.OrdinalIgnoreCase)) { continue; } if (treeView1.Nodes.IndexOfKey(drDate) == -1) { tn = new TreeNode(drDate); tn.Name = drDate; tn.Nodes.Add(tn2); treeView1.Nodes.Add(tn); } else { tn = treeView1.Nodes[treeView1.Nodes.IndexOfKey(drDate)]; tn.Nodes.Add(tn2); } }
Best Regards,
Bob Wu [MSFT]
MSDN Community Support | Feedback to us
- Contrassegnato come risposta Apurba001 venerdì 9 marzo 2012 08:14

