Here's a simple implementation of a CSV file import utility that displays the imported data in a DataGridView.
First, you will need to add the following using directives to the top of your code file:
main.cs78 chars5 lines
Then, create a method that reads the CSV file and returns a DataTable object that holds the imported data. This method can be called when the user clicks a button, for example.
main.cs845 chars34 linesThis method takes a path parameter, which is the full file path of the CSV file to import. It reads the CSV file line by line, splits each line into an array of fields using comma as the delimiter, and adds each field to a DataRow object. The DataRow object is then added to a DataTable object.
Next, create a Button control on your form and add an event handler for the Click event. In this event handler, call the GetDataTableFromCsv method and pass the file path of the CSV file to import. Set the DataSource property of the DataGridView control to the DataTable object returned by the GetDataTableFromCsv method.
main.cs201 chars7 linesFinally, run your application and click the button to import the CSV file. The imported data will be displayed in the DataGridView control.
Note: This implementation assumes that the CSV file has a header row that contains the column names. If your CSV file does not have a header row, you will need to modify the code accordingly.
gistlibby LogSnag