There is a form Form1 on which, through the designer, I create the DataGridView component. And there is a class openAndReadCSV , in which I want to open, read and write a csv-file in the List . After that, the data from the sheet to write to the DataGridView .

openAndReadCSV code

 public class openAndReadCSV : Form1 { public static void reading() { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "CSV files (*.csv) | *.csv"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { var i = 0; var reader = new StreamReader(File.OpenRead(openFileDialog1.FileName)); List<string> listA = new List<string>(); while (!reader.EndOfStream) { var line = reader.ReadLine(); var values = line.Split(';'); listA.Add(values[0]); i++; } MessageBox.Show(openFileDialog1.FileName); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } 

How can I copy data from a List to a DataGridView ?

  • one
    Apparently WinForms? openAndReadCSV - is inherited from Form1 - is it so conceived or random? If so conceived, and you need access to the elements of the parent class - just make the necessary elements protected instead of private. - rdorn 7:57 pm
  • set protected. not working - Vadim
  • "set protected. does not work" great, but what exactly does not work? I do not see in the code you refer to the DataGridView, I see only the reading of the file. - rdorn
  • Form1 frm = new Form1 (); frm.dataGrid.Rows.Add (values ​​[0]); - Vadim
  • Um ... why so? openAndReadCSV is inherited from Form1, which means it is also a form with all Form1 elements. Just write this.dataGrid.Rows.Add (values ​​[0]); (this - you can not write) - rdorn

1 answer 1

If I understand you correctly, then, as a first approximation, you need to display the contents of a CSV on a DataGridView . This task can be divided into two stages:

  1. Read the file and parse it into an object (s) convenient for further work.
  2. Display the result on the DataGridView .

In the simplest case, this does not require a separate class; it suffices to render the processing of the source file into a separate method. Moreover, this method should not know anything about how the result of its work will be displayed, or about where the file came from. In addition, we need a class to represent a single row of CSV . You can do without it, but later you will see why it is needed. Because I do not know (and you did not indicate in the question) the structure of the original CSV , we will assume that it is guaranteed to have two columns. Then the class for representing the string might look like this:

 public class CsvRow { public string FirstField { get; set; } public string SecondField { get; set; } } 

Now select from your fragment the reading and parsing into a separate method that will return List<CsvRow> as the result of processing:

 private List<CsvRow>ParseCsv(string fileName) { var result = new List<CsvRow>(); foreach(var line in File.ReadLines(fileName)) { var values = line.Split(';'); var row = new CsvRow() { FirstField = values[0], SecondField = values[1] } result.Add(row); } return result; } 

And we put the resulting method in the code of the form or a separate class with helpers. With the parser finished - display the result. Suppose we have a button, when clicking on which, the user selects a file for display.

 private void button1_Click(object sender, EventArgs args) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { dataGridView1.DataSource = ParseCsv(openFileDialog1.FileName); } } 

That's all for it.

But I promised to tell you why you need a class for data. The fact is that DataGridView does not know how to work with fields - only with properties. For each public property, a separate column will be created and one row of the grid will contain the property values ​​in the corresponding cells. Otherwise, it is necessary to fill the grid with your hands.

Alternatives

  1. Implement the INotifyPropertyChanged interface in Form1 , create a public field of type List<T> trigger the PropertyChanged event when writing values ​​to it and, in the event handler, load the sheet into the DataSource grid. It is a bit more complicated, but in some cases it is very convenient.

  2. It is not recommended to set the public access level to the public level and work with it directly. This option breaks encapsulation, I cite it only because it is also possible, but it is never recommended for practical use.

PS: The above code intends to omit the checks when working with the file and some others, in order not to overload the code and bring the main idea.