Good afternoon!
I would like to receive your suggestions on this subject.
I am making a small tool in the office to display the "Hierarchical Product Data" in a tree. Our application can only display it in tabular form, which makes it difficult to track the presence of incorrect data in the hierarchy.
I was able to do some logic and display the data in the correct hierarchy.
But my main problem here is that I am dealing with 100K-200K + entries, and it definitely takes time to make / assign each node and add it to the tree. From my test, medium nodes can be created in a minute - 8000. I also noticed that the memory usage in this application gradually increases as it starts up.
I'll talk about screenshots of the data structure and how the application looks to give you an idea:
Please review my code below and I will be very happy to know your thoughts on how to optimize this. I know that much remains to be improved.
And sorry for the long post ...
By the way, I am using C # and .net2.0.
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.Data.OleDb; using System.Data; using System.IO; using System.Threading; namespace WinformAppTest { public partial class MainForm : Form { private DataSet _ds = new DataSet(); public MainForm() { InitializeComponent(); } void MainFormLoad(object sender, EventArgs e) { PopulateDataSet(); lblTotalRows.Text = _ds.Tables[0].Rows.Count.ToString(); } void PopulateDataSet() { string query = @"select * from " + "test.csv"; try { OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\;Extended Properties=\"Text;HDR=Yes;FMT=Delimited\""); OleDbDataAdapter da = new OleDbDataAdapter(); OleDbCommand cmd = new OleDbCommand(query, conn); conn.Open(); da.SelectCommand = cmd; _ds.Clear(); da.Fill(_ds, "CSV"); conn.Close(); } catch (Exception ex) { MessageBox.Show(ex.GetBaseException().ToString()); } } void BtnRunClick(object sender, EventArgs e) { Thread newThread1 = new Thread(MainForm.DoWork); newThread1.Start(this); } public static void DoWork(object data) { MainForm form = (MainForm)data; int counter = 0; TreeNode[] nd; foreach(DataRow row in ((MainForm)data)._ds.Tables[0].Rows) { TreeNode node = new TreeNode(); if(row["Level Number"].ToString() == "1") { node.Name = row["ECC Hierarchy Code"].ToString(); node.Text = row["ECC Hierarchy Code"].ToString() + ", " + row["Name"].ToString(); form.Invoke((MethodInvoker)delegate { ((MainForm)data).treeView1.Nodes.Add(node); }); } else { nd = ((MainForm)data).treeView1.Nodes.Find(row["Parent Code"].ToString(), true); node.Name = (string)row["ECC Hierarchy Code"]; node.Text = row["ECC Hierarchy Code"].ToString() + ", " + row["Name"].ToString(); form.Invoke((MethodInvoker)delegate { nd[0].Nodes.Add(node); }); } counter++; form.Invoke((MethodInvoker)delegate { ((MainForm)data).lblLoded.Text = counter.ToString(); }); } } } }
source share