Diskuze: TreeView
Michal Žůrek - misaz:15.3.2014 12:58
musíš si sám rozparsrovaat to CSV a načíst do TreeView.
Maros2470:15.3.2014 13:07
Rozparsované to mám a zatím to načítám do listbox, ale to je nepřehledné. Do treeview to načíst zatím neumím. Můžeš mi s tím poradit?
Michal Žůrek - misaz:15.3.2014 13:16
pro WF je pěkná ukázka v MSDN library http://msdn.microsoft.com/…reeview.aspx?…
Maros2470:15.3.2014 15:44
Jsem z toho mimo (začínám).
CSV mám rozparsované takto:
public void Nacti()
{
spotrebice.Clear();
// načte soubor
using (StreamReader sr = new StreamReader(@"revize.csv", Encoding.Default))
{
string s;
// čte po řádcích
while ((s = sr.ReadLine()) != null)
{
string[] rozdeleno = s.Split(';');
string customer = rozdeleno[0];
string place_1 = rozdeleno[1];
string place_2 = rozdeleno[2];
string place_3 = rozdeleno[3];
string place_4 = rozdeleno[4];
string id = rozdeleno[5];
string name = rozdeleno[6];
string sortingKey = rozdeleno[7];
string invertoryNumber = rozdeleno[8];
string serialNumber = rozdeleno[9];
Nevím jak do TreeView dostat, asi jsem blbej. Na ten odkaz jsem koukal.
Jan Vargovský:15.3.2014 15:52
Je to normální stromová struktura. Poradil bych ti v podstatě to samé co na MSDN, takže čemu přesně nerozumíš?
Btw, kdybys ukázal celou metodu tak by to bylo super, tohle nám toho moc neřeklo.
Maros2470:15.3.2014 16:05
Asi jsem na něco přišel. Zkusím to. Když to nepůjde, zase se ozvu.
Napsal jsem to takto:
public void nacti2Button_Click(object sender, EventArgs e)
{
databaze.Nacti();
seznamTreeView.BeginUpdate();
seznamTreeView.Nodes.Clear();
seznamTreeView.Nodes.Add(new TreeNode(spotrebic.Customer));
seznamTreeView.Nodes[0].Nodes.Add(new TreeNode(spotrebic.Place_1));
seznamTreeView.Nodes[0].Nodes[0].Nodes.Add(new TreeNode(spotrebic.Place_2));
seznamTreeView.Nodes[0].Nodes[0].Nodes[0].Nodes.Add(new TreeNode(spotrebic.Place_3));
seznamTreeView.Nodes[0].Nodes[0].Nodes[0].Nodes[0].Nodes.Add(new TreeNode(spotrebic.Place_4));
seznamTreeView.Nodes[0].Nodes[0].Nodes[0].Nodes[0].Nodes[0].Nodes.Add(new TreeNode(spotrebic.Id));
seznamTreeView.EndUpdate();
}
ale vytvoří to prázdný seznam.
třídu spotřebič mám takto:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Eli_Revize_kontrola
{
public class Spotrebic
{
public string Customer { get; private set; }
public string Place_1 { get; private set; }
public string Place_2 { get; private set; }
public string Place_3 { get; private set; }
public string Place_4 { get; private set; }
public string Id { get; private set; }
public string Name { get; private set; }
public string SortingKey { get; private set; }
public string InvertoryNumber { get; private set; }
public string SerialNumber { get; private set; }
public int RevisionInterval { get; private set; }
public DateTime TakenIntoEvidence { get; private set; }
public string Note { get; private set; }
public string Group { get; private set; }
public string ProtectionClass { get; private set; }
public DateTime MeasDate { get; private set; }
public string MeasGlobalNote { get; private set; }
public string MeasGlobalStatus { get; private set; }
public string InspectorName { get; private set; }
public string InspectorSecondName { get; private set; }
public string InspectorLicense { get; private set; }
public string DeviceSerialNum { get; private set; }
public string DeviceType { get; private set; }
public string DeviceCalibrationNum { get; private set; }
public DateTime DeviceCalibrationExpire { get; private set; }
public string SupplFirm { get; private set; }
public string SupplIC { get; private set; }
public Spotrebic()
{
}
public Spotrebic(string customer, string place_1, string place_2, string place_3, string place_4, string id, string name, string sortingKey, string invertoryNumber,
string serialNumber, int revisionInterval, DateTime takenIntoEvidence, string note, string group, string protectionClass, DateTime measDate,
string measGlobalNote, string measGlobalStatus, string inspectorName, string inspectorSecondName, string inspectorLicense, string deviceSerialNum,
string deviceType, string deviceCalibratinNum, DateTime deviceCalibrationExpire, string supplFirm, string supplIC)
{
Customer = customer;
Place_1 = place_1;
Place_2 = place_2;
Place_3 = place_3;
Place_4 = place_4;
Id = id;
Name = name;
SortingKey = sortingKey;
InvertoryNumber = invertoryNumber;
SerialNumber = serialNumber;
RevisionInterval = revisionInterval;
TakenIntoEvidence =takenIntoEvidence;
Note = note;
Group = group;
ProtectionClass = protectionClass;
MeasDate = measDate;
MeasGlobalNote = measGlobalNote;
MeasGlobalStatus = measGlobalStatus;
InspectorName = inspectorName;
InspectorSecondName = inspectorSecondName;
InspectorLicense = inspectorLicense;
DeviceSerialNum = deviceSerialNum;
DeviceType = deviceType;
DeviceCalibrationNum = deviceCalibratinNum;
DeviceCalibrationExpire = deviceCalibrationExpire;
SupplFirm = supplFirm;
SupplIC = supplIC;
}
public override string ToString()
{
return Name;
}
public DateTime SpoctiRevizi()
{
DateTime naslednaRevize = MeasDate.AddMonths(+RevisionInterval);
return naslednaRevize;
}
}
}
a třídu Databaze takto:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Eli_Revize_kontrola
{
class Databaze
{
private List<Spotrebic> spotrebice;
private string soubor;
public Databaze(string soubor)
{
spotrebice = new List<Spotrebic>();
this.soubor = soubor;
}
public void PridejSpotrebic(string customer, string place_1, string place_2, string place_3, string place_4, string id, string name, string sortingKey, string invertoryNumber,
string serialNumber, int revisionInterval, DateTime takenIntoEvidence, string note, string group, string protectionClass, DateTime measDate,
string measGlobalNote, string measGlobalStatus, string inspectorName, string inspectorSecondName, string inspectorLicense,
string deviceSerialNum, string deviceType, string deviceCalibratinNum, DateTime deviceCalibrationExpire, string supplFirm, string supplIC)
{
Spotrebic u = new Spotrebic(customer, place_1, place_2, place_3, place_4, id, name, sortingKey, invertoryNumber, serialNumber, revisionInterval, takenIntoEvidence,
note, group, protectionClass, measDate, measGlobalNote, measGlobalStatus, inspectorName, inspectorSecondName, inspectorLicense,
deviceSerialNum, deviceType, deviceCalibratinNum, deviceCalibrationExpire, supplFirm, supplIC);
spotrebice.Add(u);
}
public Spotrebic[] VratVsechny()
{
return spotrebice.ToArray();
}
public void Uloz()
{
using (StreamWriter sw = new StreamWriter(soubor))
{
// projetí uživatelů
foreach (Spotrebic sp in spotrebice)
{
// vytvoření pole hodnot
string[] hodnoty = { sp.Customer, sp.Place_1, sp.Place_2, sp.Place_3, sp.Place_4, sp.Id, sp.Name, sp.SortingKey, sp.InvertoryNumber, sp.SerialNumber,
sp.RevisionInterval.ToString(), sp.TakenIntoEvidence.ToShortDateString(), sp.Note, sp.Group, sp.ProtectionClass, sp.MeasDate.ToShortDateString(),
sp.MeasGlobalNote, sp.MeasGlobalStatus, sp.InspectorName, sp.InspectorSecondName, sp.InspectorLicense, sp.DeviceSerialNum, sp.DeviceType,
sp.DeviceCalibrationNum, sp.DeviceCalibrationExpire.ToShortDateString(), sp.SupplFirm, sp.SupplIC};
// vytvoření řádku
string radek = String.Join(";", hodnoty);
// zápis řádku
sw.WriteLine(radek);
}
// vyprázdnění bufferu
sw.Flush();
}
}
public void Nacti()
{
spotrebice.Clear();
// načte soubor
using (StreamReader sr = new StreamReader(@"revize.csv", Encoding.Default))
{
string s;
// čte po řádcích
while ((s = sr.ReadLine()) != null)
{
string[] rozdeleno = s.Split(';');
string customer = rozdeleno[0];
string place_1 = rozdeleno[1];
string place_2 = rozdeleno[2];
string place_3 = rozdeleno[3];
string place_4 = rozdeleno[4];
string id = rozdeleno[5];
string name = rozdeleno[6];
string sortingKey = rozdeleno[7];
string invertoryNumber = rozdeleno[8];
string serialNumber = rozdeleno[9];
int revisionInterval = int.Parse(rozdeleno[10]);
DateTime takenIntoEvidence = DateTime.Parse(rozdeleno[11]);
string note = rozdeleno[12];
string group = rozdeleno[13];
string protectionClass = rozdeleno[14];
DateTime measDate = DateTime.Parse(rozdeleno[15]);
string measGlobalNote = rozdeleno[16];
string measGlobalStatus = rozdeleno[17];
string inspectorName = rozdeleno[18];
string inspectorSecondName = rozdeleno[19];
string inspectorLicense = rozdeleno[20];
string deviceSerialNum = rozdeleno[21];
string deviceType = rozdeleno[22];
string deviceCalibratinNum = rozdeleno[23];
DateTime deviceCalibrationExpire = DateTime.Parse(rozdeleno[24]);
string supplFirm = rozdeleno[25];
string supplIC = rozdeleno[26];
PridejSpotrebic(customer, place_1, place_2, place_3, place_4, id, name, sortingKey, invertoryNumber, serialNumber, revisionInterval, takenIntoEvidence,
note, group, protectionClass, measDate, measGlobalNote, measGlobalStatus, inspectorName, inspectorSecondName, inspectorLicense,
deviceSerialNum, deviceType, deviceCalibratinNum, deviceCalibrationExpire, supplFirm, supplIC);
}
}
}
}
}
David Hartinger:15.3.2014 18:31
To je nějaké divné. Já jsem TreeView vždy používal takto:
TreeNode spotrebicNode = seznamTreeView.Nodes.Add(spotrebic.Customer);
TreeNode spotrebicPlace1 = spotrebicNode.Nodes.Add(...)
Maros2470:15.3.2014 18:39
Zkusil jsem to napsat podle tebe, ale je to prázdné. Vidím jen jak jsou vytvořeny Nodes, ale nevidím co v nich je. Není možné, že mám blbě nastavenou přímo komponentu TreeView?
David Hartinger:15.3.2014 18:43
A máš opravdu něco v těch vlastnostech? Zkus napsat jen:
seznamTreeView.Nodes.Add("Uzel");
Jestli ho uvidíš. Když to nepůjde, tak máš fakt blbě TreeView a dej si na form nový. Když to půjde, tak nemáš v modelu data.
Maros2470:15.3.2014 18:53
To jsem už zkoušel a funguje to. Takže je to daty. Nevím tam z csv dostat naparsovaná data. Csv mám načteno viz. výše. Zatím jsem to načítal do listbox
public partial class Form1 : Form
{
class Spotrebic
{
public string Name { get; set; }
public string Customer { get; set; }
public DateTime Date { get; set; }
}
public Form1()
{
InitializeComponent();
List<Spotrebic> spotrebice = new List<Spotrebic>();
spotrebice.AddRange(new Spotrebic[] {
new Spotrebic{ Name="sp1", Customer = "cust1", Date = DateTime.Now},
new Spotrebic{ Name="sp2", Customer = "cust2", Date = DateTime.Now},
new Spotrebic{ Name="sp3", Customer = "cust3", Date = DateTime.Now}
});
List<TreeNode> nodes = new List<TreeNode>();
foreach (var sp in spotrebice)
{
TreeNode spotrebic = new TreeNode(sp.Name);
spotrebic.Nodes.AddRange(new TreeNode[]
{
new TreeNode(sp.Customer),
new TreeNode(sp.Date.ToShortDateString())
});
nodes.Add(spotrebic);
}
treeView1.Nodes.AddRange(nodes.ToArray());
}
}
Tohle mi funguje
David Hartinger:15.3.2014 18:57
Z toho kódu kde parsuješ CSV vidím, že dáváš data do nějakých stringových proměnných a ne do vlastností instance
Jan Vargovský:15.3.2014 19:07
A potom volá metodu PridejSpotrebic kde se už ta instance vytvoří a přidá do seznamu.
David Hartinger:15.3.2014 19:15
Nojo, já se díval na ten první kód. V tom případě nevím, kde se bere ta proměnná spotrebic v obsluze tlačítka. Řešení je velmi jednoduché a to naučit se používat breakpointy a zobrazování obsahu proměnných. Pak tu chybu zjistí hned. Breakpoint je myslím F9 nebo klik na číslo řádku.
Maros2470:15.3.2014 19:27
Můžeš poradit kde k tomu cos napsal něco najdu. Nechci jenom něco kopírovat, ale naučit se.
David Hartinger:15.3.2014 19:33
Můžeš začít tím, že zmáčkneš tu klávesu F9 Umístíš tím breakpoint na určitý řádek. Když program spustíš, tak se na něm zastaví a ty můžeš myší najíždět na proměnné a dívat se co v nich je.
Jan Vargovský:15.3.2014 20:30
Však neposílal kód formuláře (už přestanu )
Zobrazeno 27 zpráv z 27.