D
If I've understood you correctly, you need to look at the contents in the first approach. CSV ♪ DataGridView♪ This can be divided into two stages:Read the file and evaporate it into a suitable facility for further work.Describe the result to DataGridView♪In the simplest case, this does not require a separate class, but it is sufficient to make the reference file a separate method. However, this method should not know how its work will be displayed or where the file came from. Besides, we're gonna need a class to present one line. CSV♪ You could use it without him, but you'll see why you need it later. I don't know (and you didn't point out) the structure of the baseline. CSVLet's assume there are two pillars in it. Then the line class might look like:public class CsvRow
{
public string FirstField { get; set; }
public string SecondField { get; set; }
}
Now we're going to separate reading and parsing from your fragment into a separate method that will return List<CsvRow> as a result of treatment: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 put the method in the form code or separate class with the choppers. We're done with the password, we'll show the results. Let us have a button on which the user chooses a display file.private void button1_Click(object sender, EventArgs args)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
dataGridView1.DataSource = ParseCsv(openFileDialog1.FileName);
}
}
That's all.But I promised to tell you why I needed a data class. The thing is, DataGridView He doesn't know how to work with the fields. Only with properties. A separate column will be created for each public characteristic and one line of the dried will contain values in the respective cells. Otherwise, it's gonna have to be done by hand.AlternativesRealize Form1 Interface INotifyPropertyChangedcreate a public field like List<T> event PropertyChanged on the record and, in the processor of the event, download the sheet. DataSource Grida. It's a little more complicated, but in some cases it's very convenient.Not recommended To challenge the hybrid itself the level of access public and work directly with him. This option violates the collection, brings it only because it is also possible, but it is not categorically recommended for practical application ever.PS: In the code, it is the intention of deleting the files and some others so that the code is not compromised and the main idea is presented.