none
Reading the CSV file and extracting in the listBox RRS feed

  • Question

  • Hello there,

    I have a CSV file which contains different tests.

    I want read that CSV file and display only the tests column in the listBox.

    I have done upto browsing the file and getting the strings of the directory to the textbox.

    Now i need to read this CSV file and display the tests in the listbox.

    Can anyone help me please.

    Thank you


    Akshay

    Thursday, March 14, 2019 12:43 PM

Answers

  • Hi

    Thank you for posting here.

    According to your description, you want to read the CSV file and extract it in the listBox.

    You could try the following code.

    private void button1_Click(object sender, EventArgs e)
    
            {
    
                List<example> values = File.ReadAllLines("d:\\test.csv")
    
                                              .Skip(1)
    
                                              .Select(v => example.FromCsv(v))
    
                                              .ToList();
    
                foreach (var item in values)
    
                {
    
                    listBox1.Items.Add(item.t1);
    
                }
    
            }
    
    public class example
    
        {
    
            public int t1;
    
            public string t2;
    
            public string t3;
    
            public static example FromCsv(string csvLine)
    
            {
    
                string[] values = csvLine.Split(',');
    
                example dailyValues = new example();
    
                dailyValues.t1 = Convert.ToInt32(values[0]);
    
                dailyValues.t2 = values[1];
    
                dailyValues.t3 = values[2];
    
                return dailyValues;
    
            }
    
    }

    CSV:

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.







    Friday, March 15, 2019 6:21 AM
    Moderator

All replies

  • You can use TextFieldParser to read/parse the .csv file

    Thursday, March 14, 2019 1:12 PM
  • Hello,

    TextFieldParser usually suits most common reads while for advance operations CsvHelper is an option.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, March 14, 2019 1:37 PM
    Moderator
  • I need that in listbox.

    I stll cant figure it out.

    As i am very new to this C# programming


    Akshay

    Friday, March 15, 2019 5:20 AM
  • Hi

    Thank you for posting here.

    According to your description, you want to read the CSV file and extract it in the listBox.

    You could try the following code.

    private void button1_Click(object sender, EventArgs e)
    
            {
    
                List<example> values = File.ReadAllLines("d:\\test.csv")
    
                                              .Skip(1)
    
                                              .Select(v => example.FromCsv(v))
    
                                              .ToList();
    
                foreach (var item in values)
    
                {
    
                    listBox1.Items.Add(item.t1);
    
                }
    
            }
    
    public class example
    
        {
    
            public int t1;
    
            public string t2;
    
            public string t3;
    
            public static example FromCsv(string csvLine)
    
            {
    
                string[] values = csvLine.Split(',');
    
                example dailyValues = new example();
    
                dailyValues.t1 = Convert.ToInt32(values[0]);
    
                dailyValues.t2 = values[1];
    
                dailyValues.t3 = values[2];
    
                return dailyValues;
    
            }
    
    }

    CSV:

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.







    Friday, March 15, 2019 6:21 AM
    Moderator
  • Is this not forms or wpf related?
    Friday, March 15, 2019 9:43 AM
  • Thank you so much. 

    That was really helpful ..


    Akshay

    Friday, March 15, 2019 9:45 AM