c# - Load .txt data files to DataGridView -
i have code open point file , read data files:
private void cmdload_click(object sender, eventargs e) { stream mystream = null; openfiledialog openfiledialog1 = new openfiledialog(); openfiledialog1.initialdirectory = "\\yamaha"; openfiledialog1.filterindex = 2; openfiledialog1.restoredirectory = true; if (openfiledialog1.showdialog() == dialogresult.ok) { try { if ((mystream = openfiledialog1.openfile()) != null) { using (mystream) { string filename = openfiledialog1.filename; using (var reader = file.opentext(@filename)) { string line; while ((line = reader.readline()) != null) { //do fetch data , paste gridview operation } } } } } catch (exception ex) { messagebox.show("error: not read file disk. original error: " + ex.message); } } }
now stuck in while loop. want loop go through file , fetch data , paste on gridview.
my gridview like:
my text file like:
if file not big, simple using linq. need read lines parse each line , set gridview's datasource.
private void cmdload_click(object sender, eventargs e) { openfiledialog openfiledialog1 = new openfiledialog(); openfiledialog1.initialdirectory = "\\yamaha"; openfiledialog1.filterindex = 2; openfiledialog1.restoredirectory = true; if (openfiledialog1.showdialog() == dialogresult.ok) { try { string[] filelines= system.io.file.readalllines(openfiledialog1.filename); ienumerable<string[]> splitedlines = filelines.select(c => c.split(new string[]{" "}, stringsplitoptions.removeemptyentries)); var data = splitedlines.select(c => new { point = c[0], x = c[2],//c[1] "=" sign y = c[3], z = c[4], r = c[5], = c[6], b = c[7], c = c[8] }); datagridview1.datasource = data.toarray(); } catch (exception ex) { messagebox.show("error: not right. original error: " + ex.message); } } }
Comments
Post a Comment