c# - Load text to datagridview -


i have point files like:
example point file

i have created code allows users select files dialog box , populate data gridview.

private void cmdload_click(object sender, eventargs e) {     datatable table = new datatable();     table.columns.add("point");     table.columns.add("x");     table.columns.add("y");     table.columns.add("z");     table.columns.add("r");     table.columns.add("a");     table.columns.add("b");     table.columns.add("c");     stream mystream = null;     openfiledialog openfiledialog1 = new openfiledialog();      openfiledialog1.initialdirectory = "c:\\";      openfiledialog1.filter = "data files (*.pnt)|*.pnt";     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)                         {                             string[] parts = line.split(' ');                             table.rows.add(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], parts[6], parts[7]);                         }                         datagridview1.datasource = table;                     }                 }             }         }          catch (exception ex) // need add catch block if yo using try block         {             messagebox.show("error: not read file disk. original error: " + ex.message);         }      } }   

this code works fine, datagridview not display value.

example output

if not creating datagridview columns through code have set datapropertyname of datagridviewcolumn columns ids in datatable.

sample code:

datagridview1.autogeneratecolumns = false; datagridviewtextboxcolumn col1 = new datagridviewtextboxcolumn(); col1.name = "point"; col1.headertext = "point"; col1.datapropertyname = "point"; datagridview1.columns.add(col1) 

the other way set autogeneratecolumns property true create required columns automatically , bind data datatable. don't have create columns through code in above sample code.

datagridview1.autogeneratecolumns = true; 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -