none
Question about textfieldparser and getting specific lines. RRS feed

  • Question

  • I'm using the below lines to grab the first line of a CSV file and put each field into a list box. That's worked well till now. But now I need to be able to move up and down in the file displaying each row of fields. I'm uncertain on how to modify my code to accomplish this. 

    using (TextFieldParser csvParser = new TextFieldParser(csvFile))
    {
        csvParser.CommentTokens = new string[] { "#" };
        csvParser.SetDelimiters(new string[] { "," });
        csvParser.HasFieldsEnclosedInQuotes = true;
    
        fields = csvParser.ReadFields();
        listBoxFieldsFound.DataSource = fields;
    }

    Is what I want possible with the above code, or do i need a whole new approach. Thanks


    • Edited by ASI Tech Tuesday, October 3, 2017 4:55 PM
    Tuesday, October 3, 2017 4:51 PM

Answers

  • Hello ASI Tech,

    >>But now I need to be able to move up and down in the file displaying each row of fields

    If you want to get certain row values,you could use TextFieldParser.LineNumber property,you code should be like this.

            private void button2_Click(object sender, EventArgs e)
            {
    
                string csvfilepath = "D:/T3.csv";
                using (TextFieldParser csvParser = new TextFieldParser(csvfilepath))
                {
                    while (!csvParser.EndOfData)
                    {
                        csvParser.CommentTokens = new string[] { "#" };
                        csvParser.SetDelimiters(new string[] { "," });
                        csvParser.HasFieldsEnclosedInQuotes = true;
    
                        string[] fields = csvParser.ReadFields();
                        if (csvParser.LineNumber == 3)
                        {
                            listBox1.DataSource = fields;
                            return;
                        }
                    }
                }
             }
    

    Sincerely,

    neil hu


    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.

    • Marked as answer by ASI Tech Friday, October 6, 2017 3:40 PM
    Thursday, October 5, 2017 7:55 AM
    Moderator

All replies

  • Consider the DataGridView control. Add one to the form, then try this code:

    var dt = new DataTable();
    
    if( !csvParser.EndOfData )
    {
       csvParser.ReadFields().All( f => { dt.Columns.Add( f ); return true; } );
    
       while( !csvParser.EndOfData )
       {
          dt.Rows.Add( csvParser.ReadFields() );
       }
    }
    
    dataGridView1.DataSource = dt;
    


    • Edited by Viorel_MVP Tuesday, October 3, 2017 6:01 PM
    Tuesday, October 3, 2017 6:00 PM
  • Well I'm using the listbox to drag items to other controls. Textboxs in this case. The main goal is to match fields in a CSV with fields in a database. So a DataGridView was really what i was looking for. In a perfect situation, all the fields would have headers. Not so in this case. And since not all fields have entries, thats why i want to be able to choose other lines. But thank you!
    Tuesday, October 3, 2017 8:12 PM
  • Hello ASI Tech,

    >>But now I need to be able to move up and down in the file displaying each row of fields

    If you want to get certain row values,you could use TextFieldParser.LineNumber property,you code should be like this.

            private void button2_Click(object sender, EventArgs e)
            {
    
                string csvfilepath = "D:/T3.csv";
                using (TextFieldParser csvParser = new TextFieldParser(csvfilepath))
                {
                    while (!csvParser.EndOfData)
                    {
                        csvParser.CommentTokens = new string[] { "#" };
                        csvParser.SetDelimiters(new string[] { "," });
                        csvParser.HasFieldsEnclosedInQuotes = true;
    
                        string[] fields = csvParser.ReadFields();
                        if (csvParser.LineNumber == 3)
                        {
                            listBox1.DataSource = fields;
                            return;
                        }
                    }
                }
             }
    

    Sincerely,

    neil hu


    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.

    • Marked as answer by ASI Tech Friday, October 6, 2017 3:40 PM
    Thursday, October 5, 2017 7:55 AM
    Moderator
  • Excellent. Thank you!
    Friday, October 6, 2017 3:40 PM