c# - Display TextFile to listView -
i trying display opened textfile in textboxes , listviews. every line on textfile represents value.
here have done now:
public void openfile() { openfiledialog openfiledialog = new openfiledialog(); openfiledialog.filter = "text files (*.txt)|*.txt|all files (*.*)|*.*"; string line = ""; int index = 0; if (openfiledialog.showdialog() == true) using (streamreader sr = file.opentext(openfiledialog.filename)) { while ((line = sr.readline()) != null) { index++; if (index == 1) invoicenumbertxt.text = line; else if (index == 2) invoicedatetxt.text = line; else if (index == 3) duedatetxt.text = line; else if (index == 4 || index == 5 || index == 6 || index == 7 || index == 8 || index == 9) personinfolst.items.add(line); else if (index == 10) { system.windows.messagebox.show(index.tostring()); items.add(new itemproperties { item = line, <<-- //problem here. index never 11. description = (index == 11) ? line : "" }); itemlst.itemssource = items; } else break; } } }
index
convenient flag (variable) insert lines in order, , not overlap multiple lines same control.
everything seems work except can't figure out how add different columns same row on listview
.
normally when user adding different columns same row, user uses this:
items.add(new itemproperties { item= "3", description = "strong bear", publisher = "blizzard" });
all column inside same itemproperties
property included same row.
i want to achieve same thing need check index
each value added. far worked can't increase index
inside else if
statement add multiple columns same row
assuming file is required contain @ least first 3 lines in order valid, can easier:
public void openfile() { openfiledialog openfiledialog = new openfiledialog(); openfiledialog.filter = "text files (*.txt)|*.txt|all files (*.*)|*.*"; if (openfiledialog.showdialog() && file.exists(openfiledialog.filename)) { // read lines file , store them in array. var lines = file.readalllines(openfiledialog.filename); // sanity check: there must @ least 3 lines. if (lines.length < 3) system.windows.messagebox.show("error: not enough lines in file: " + openfiledialog.filename); else { invoicenumbertxt.text = lines[0]; invoicedatetxt.text = lines[1]; duedatetxt.text = lines[2]; foreach (var l in lines.skip(3).take(6)) personinfolst.items.add(l); // assuming `itemproperties` follow, groups of 3 lines, // consisting of `item`, `description`, `publisher` (var = 9; + 2 < lines.length; += 3) { items.add(new itemproperties { item = lines[i], description = lines[i + 1], publisher = lines[i + 2] }); } itemlst.itemssource = items; } } }
Comments
Post a Comment