How to read csv file in c# windows application




















From there, you can apply to your real projects. Windows Forms: How to read. C csv to datatable oledb Creating a ReadCsv method that allows you to read data from csv file into DataTable in c. OK dataGridView. Show ex. Linq; using System. Text; using System. Tasks; using System. Forms; using System. Add "Username" ; dataTable.

Add "Password" ; dataTable. ReadAllLines filePath. Split ',' ; while! Split ',' ; dataTable. Yash Saraiya. Yash Saraiya Yash Saraiya 1, 1 1 gold badge 20 20 silver badges 39 39 bronze badges. Are you checking autogeneratecolumns on DataGridView? So far I haven't checked it — Yash Saraiya. ReallAllLines path and get rid of the stream reader and then just loop over the contents and split the line there. Did you debug and found any problem? You might need a dataGridView1.

DataBind at the end. CSV files are more complicated than that, for instance, what if the comma is in quotes? Show 1 more comment. Active Oldest Votes. Add Fields[i]. Mohammed Ali Mohammed Ali 3 3 gold badges 9 9 silver badges 22 22 bronze badges. This is not having any effect on the dataGridView not getting any error msg or warning though — Yash Saraiya.

My datagrid name is same as displayed in the properties window. Can you help me with Try catch code? I am marking your answer as accepted because your logic works too!!! In order to parse such a different "CSV" file, we need to configure the parser.

We tell the reader that there is no header and that the comment character is. We allow comments in the file and set the comment's character. Actually, we don't have to set the comment character because by default the is used. We set the delimiter to semicolon character. The blank lines are ignored by default. In the CsvConfiguration , we set the ShouldQuote property to return true for the second field.

A new record is started with NextRecord.



0コメント

  • 1000 / 1000