none
C# Writing specific data from a .CSV into a textbox

    Question

  • I currently got the following code, this will import data from the CSV to a Textbox.

    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    
    namespace TESTEST123
    {
        public partial class Form1 : Form
        {
            OpenFileDialog openFile = new OpenFileDialog();
            string line = "";
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                if(openFile.ShowDialog() == DialogResult.OK)
                {
                    StreamReader sr = new StreamReader(openFile.FileName);
                    while(line != null)
                    {
                        line = sr.ReadLine();
                        if(line != null)
                        {
                            textBox1.AppendText(line);
                        }
                    }
                    sr.Close();
                }
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                openFile.Filter = "CSV Files (.csv) | *.csv";
            }
         }
    }

    But instead of reading all the info from the CSV into a Textbox, I want to write a specific value to a textbox. 

    The excel file looks like the following: 
    (THIS IS THE FILE THAT GET'S OPENED IN THE PROGRAM)

    If I open it with NotePad it looks like this: 

    Let's say I want to read the parameter from row 5 column C into a textbox, how can I do this? I've only been using C# for 2 days and Ive been stuck on this for quite some time. Anyone who can help me out?

    Basicly I have to write a program to represent the HMI of a machine, so they read the CSV file into the program, edit parameters, and write it back.

    Thanks in advance!



    • Edited by Nikoww Friday, November 23, 2018 12:33 PM
    Friday, November 23, 2018 10:37 AM

All replies

  • Hello, 

    First of all - your file is not a "flat" file - so, it not a CSV file. 

    Most likely the simplest way will be to get flat file and load your data into DataTable and do what you need to do with table, rows and columns.

    Second option - in VB.Net is a class to read an CSV and access fields in reader. I new that it is there, but I do not remember name of the dll and name of the class. Can look on this at Monday.


    Sincerely, Highly skilled coding monkey.

    Friday, November 23, 2018 5:29 PM
  • I'll check it out
    Monday, November 26, 2018 7:43 AM
  • Hello, 

    First of all - your file is not a "flat" file - so, it not a CSV file. 

    Most likely the simplest way will be to get flat file and load your data into DataTable and do what you need to do with table, rows and columns.

    Second option - in VB.Net is a class to read an CSV and access fields in reader. I new that it is there, but I do not remember name of the dll and name of the class. Can look on this at Monday.


    Sincerely, Highly skilled coding monkey.

    At 'Type' it says 'Comma Seperated Values File' 

    And I dont want to use a DataTable, I have to represent a HMI screen, so I need specific values in specific TextBoxes.

    I'd like to see this class yes

    Kind Regards


    Monday, November 26, 2018 8:10 AM
  • If you change an extension (and only extension) on your file to an .XML - would it came to be an XML file? 

    How do determine a definition for file with extension .C7LD89?

    -----

    Assembly: Microsoft.VisualBasic.dll

    Class: Microsoft.VisualBasic.FileIO.TextFieldParser

    You must have a flat file to use this reader.


    Sincerely, Highly skilled coding monkey.

    Monday, November 26, 2018 8:46 AM