c# - Reading a file, write to diffrent table if (streamwriter =...) -


i've got little problem readin file datagridviews (3 more specific).

using (streamreader sr = new streamreader(fn)) {     string[] datarow; int tableno=0;     colt = sr.readline();     console.out.writeline(colt);      if (colt == "@table")     {         tableno++;//here tableno equals 1         colt = sr.readline();        // console.out.writeline(colt);        // console.out.writeline(tableno);     }      switch (tableno)     {       case 1:                   columns = colt.split(';');     count = columns.length;      foreach (string c in columns)     {         table1.columns.add(c, typeof(string));     }      while (!sr.endofstream)     {          rowt = sr.readline();         if (rowt == "@table") break; //here program supposed break case, go , increase tableno         datarow = rowt.split(';');         table1.rows.add(datarow);      }     datagridview1.datasource = table1;     sr.close();              break;           case 2:                  columns = colt.split(';');     count = columns.length;      foreach (string c in columns)     {         table2.columns.add(c, typeof(string));     }      while (!sr.endofstream)     {         rowt = sr.readline();         if (rowt == "@table") break;         datarow = rowt.split(';');         table2.rows.add(datarow);     }     datagridview2.datasource = table2;     sr.close();             break;           case 3:                   columns = colt.split(';');     count = columns.length;      foreach (string c in columns)     {         table3.columns.add(c, typeof(string));     }      while (!sr.endofstream)     {         rowt = sr.readline();         if (rowt == "@table") break;         datarow = rowt.split(';');         table3.rows.add(datarow);     }     datagridview3.datasource = table3;     sr.close();              break; } } } 

my text file looking like:

@table k1;k2;k3 .... .. @table k1;k4 .. .. @table k1;k5 

and after running code, data appearing in gridview1. variable tableno didn't want increase. have idea?

you can make existing code work creating while loop inside using scope streamreader, , stop closing reader after completing table table (the reader closed using statement, unnecessary anyway). problem @ moment see start of next table or end of old 1 (different in answer , question..) break out of reading loop table, there's nothing change flow "go up" , start working on next one, it'll exit method @ point.

however, should more fundamentally change approach it's repetitive. @ moment you've got 3 tables, if had n tables? it's bad practice repeat code, maintenance, testing , extensibility perspective. if format of file changes example, you're going have change in 3 places, , test 3 times as if you'd written once.

here's example of how write handle number of tables , re-use same logic read each table:

private list<datatable> readdatatables(string filename) {     list<datatable> tables = new list<datatable>();     datatable currenttable = null;      using (streamreader reader = new streamreader(filename))     {            while (!reader.endofstream)         {                                 var currentline = reader.readline(); // next line file             if (string.isnullorwhitespace(currentline)) continue; // ignore blank rows              // skip rows marking end of table, reset current table             if (currentline.startswith("@endtable", stringcomparison.ordinalignorecase))             {                 currenttable = null;                 continue;             }              // initialize new table             if (currentline.startswith("@table", stringcomparison.ordinalignorecase))             {                 currenttable = new datatable();                 tables.add(currenttable);                   // initialise columns                 var columns = reader.readline().split(new char[] { ';' }, stringsplitoptions.removeemptyentries);                 foreach (var column in columns)                     currenttable.columns.add(column, typeof(string));                  continue;             }              if (currenttable == null) continue; // file not in format expecting              // add data row             var datarow = currenttable.newrow();             datarow.itemarray = currentline.split(new char[] { ';' });             currenttable.rows.add(datarow);         }     }      return tables; } 

you use button click event handler follows;

var tables = readdatatables(filename); datagridview1.datasource = tables[0]; datagridview2.datasource = tables[1]; datagridview3.datasource = tables[2]; 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -