c# - Import CSV to Gridview -
please help!
i'm trying import data csv grid before saving data database... problem no matter try, lines reading garbage or format don't know. first time i'm trying import asp.net gridview it's not working. if can please direct me in right way appreciate it!
this i've done:
private object readtoend(string filepath) { datatable dtdatasource = new datatable(); string[] filecontent = file.readalllines(filepath); if (filecontent.count() > 0) { string[] columns = filecontent[0].split(','); (int = 0; < columns.count(); i++) { dtdatasource.columns.add(columns[i]); } (int = 1; < filecontent.count(); i++) { string[] rowdata = filecontent[i].split(','); dtdatasource.rows.add(rowdata); } } gvtest.datasource = dtdatasource; gvtest.databind(); }
i need 1 column entire csv serialno. after receive correct information bind rest of data before inserting database(sql)...
this it's reading:
after reading data gives me error message of : "input array longer number of columns in table."
i've tried doing way:
private static datatable getdatatabletfromcsvfile(string path) { datatable csvdata = new datatable(); csvdata.columns.add("serialno", typeof(string)); // csvdata.columns.add("machinedetailsref", typeof(int)); try { using (textfieldparser csvreader = new textfieldparser( path)) { csvreader.setdelimiters(new string[] { "," }); csvreader.hasfieldsenclosedinquotes = true; string[] colfields = csvreader.readfields(); foreach (string column in colfields) { datacolumn serialno = new datacolumn(column); serialno.allowdbnull = true; csvdata.columns.add(serialno); } while (!csvreader.endofdata) { string[] fielddata = csvreader.readfields(); //making empty value null (int = 0; < fielddata.length; i++) { if (fielddata[i] == "") { fielddata[i] = null; } } csvdata.rows.add(fielddata); } } } catch (exception ex) { } return csvdata; }
this gives out exact same data previous attempt:
![enter image description here][2]
but in method receive no errors , grid gets populated as:
any appreciated! thanks
edit -------------
this csv file:
note: if csv file encoding different ansi? should use streamreader
following:
streamreader mystreamreader = new streamreader(path, system.text.encoding.unicode);
otherwise, have made few changes in code. see below:
private static datatable getdatatabletfromcsvfile(string path) { datatable csvdata = new datatable(); try { using (textfieldparser csvreader = new textfieldparser( path)) { csvreader.setdelimiters(new string[] { "," }); csvreader.hasfieldsenclosedinquotes = true; string[] colfields = csvreader.readfields(); foreach (string column in colfields) { datacolumn serialno = new datacolumn(column); serialno.allowdbnull = true; csvdata.columns.add(serialno); } while (!csvreader.endofdata) { string[] fielddata = csvreader.readfields(); datarow dr = csvdata.newrow(); //making empty value empty (int = 0; < fielddata.length; i++) { if (fielddata[i] == null) fielddata[i] = string.empty; dr[i] = fielddata[i]; } csvdata.rows.add(dr); } } } catch (exception ex) { } return csvdata; }
Comments
Post a Comment