Advertisement
Gorcupt

Untitled

Jan 23rd, 2020
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Data.SQLite;
  12. using System.Diagnostics;
  13. using System.Numerics;
  14.  
  15. namespace WindowsFormsApp4
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.             treeView1.BeforeExpand += treeView1_BeforeExpand;
  23.             treeView1.BeforeCollapse += treeView1_BeforeCollapse;
  24.             LoadData();
  25.         }
  26.  
  27.         public void LoadData()
  28.         {
  29.             using (var sqlcon = new SQLiteConnection(@"DataSource=A:\sqlite\321.db"))
  30.             {
  31.                 sqlcon.Open();
  32.                 using (var sqlcommand = new SQLiteCommand(sqlcon))
  33.                 {
  34.                     sqlcommand.CommandText = @"SELECT students.name_students wName
  35.     ,students.id_students idW
  36.     ,department.name_department cName
  37.     ,department.id_department idC
  38.     ,faculty.name_faculty coName
  39. FROM faculty LEFT OUTER
  40.                 JOIN department ON faculty.id_faculty = department.id_faculty LEFT OUTER
  41.                 JOIN students ON department.id_department = students.id_department
  42.  
  43. ORDER BY coName, idC, cName, idW, wName";
  44.                     using (var reader = sqlcommand.ExecuteReader())
  45.                     {
  46.                         var lastAddedCountryNode = new TreeNode();
  47.                         var lastAddedCompanyNode = new TreeNode();
  48.                         var  lastIdCompany  = -1;
  49.                         while (reader.Read())
  50.                         {
  51.                             if (!reader.IsDBNull(reader.GetOrdinal("coName")))
  52.                             {
  53.                                 var countryName = (string) reader["coName"];
  54.                                 if (countryName != lastAddedCountryNode.Text)
  55.                                 {
  56.                                     lastAddedCountryNode = treeView1.Nodes.Add(countryName);
  57.                                     lastAddedCountryNode.Tag = countryName;
  58.                                 }
  59.                             }
  60.  
  61.                             if (!reader.IsDBNull(reader.GetOrdinal("cName")))
  62.                             {
  63.                                 var idCompany = Convert.ToInt32(reader["idC"]);
  64.                                
  65.                                 if (idCompany != lastIdCompany)
  66.                                 {
  67.                                     var companyName = (string) reader["cName"];
  68.                                     lastAddedCompanyNode = lastAddedCountryNode.Nodes.Add(companyName);
  69.                                     lastAddedCompanyNode.Tag = false;
  70.                                     lastIdCompany = idCompany;
  71.                                 }
  72.                             }
  73.  
  74.                             if (reader.IsDBNull(reader.GetOrdinal("wName")))
  75.                             {
  76.                                 continue;
  77.                             }
  78.  
  79.                             lastAddedCompanyNode.Tag = true;
  80.                             var workerName = (string) reader["wName"];
  81.                             lastAddedCompanyNode.Nodes.Add(workerName);
  82.                         }
  83.                     }
  84.                 }
  85.             }
  86.  
  87.         }
  88.  
  89.         private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
  90.         {
  91.             if (e.Node.Level != 0)
  92.             {
  93.                 return;
  94.             }
  95.  
  96.             int sumA = 0, sumB = 0;
  97.             foreach (TreeNode lastAddedCompanyNode in e.Node.Nodes)
  98.             {
  99.                 if ((bool)lastAddedCompanyNode.Tag)
  100.                 {
  101.                     sumA += 1;
  102.                 }
  103.                 else
  104.                 {
  105.                     sumB += 1;
  106.                 }
  107.             }
  108.  
  109.             e.Node.Text += @" " + sumA + @" / " + sumB;
  110.         }
  111.  
  112.         private void treeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
  113.         {
  114.             if (e.Node.Level == 0)
  115.             {
  116.                 e.Node.Text = (string)e.Node.Tag;
  117.             }
  118.         }
  119.  
  120.  
  121.         private void Form1_Load(object sender, EventArgs e)
  122.         {
  123.             throw new System.NotImplementedException();
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement