Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8318945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:09:06+00:00 2026-06-08T22:09:06+00:00

Below is my sample text file { Here is my schema file [Sample File.txt]

  • 0

Below is my sample text file

enter image description here
{

Here is my schema file

[Sample File.txt]
ColNameHeader=True
Format=TabDelimited
CharacterSet=ANSI

And here is the code i have so far writen to try and read the above sample file, the data rows read from the text file above is supposed to be returned for display in a dataGridView control. The problem is, its being returned as single column, yet i want to use those white spaces as the column delimiters. I have tried different character delimiters with out success.

public DataSet LoadCSV(int numberOfRows)
    {
        DataSet ds = new DataSet();
            // Creates and opens an ODBC connection
            string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + this.dirCSV.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";

            string sql_select;
            OdbcConnection conn;
            conn = new OdbcConnection(strConnString.Trim());
            conn.Open();

            //Creates the select command text
            if (numberOfRows == -1)
            {
                sql_select = "select * from [" + this.FileNevCSV.Trim() + "]";
            }
            else
            {
                sql_select = "select top " + numberOfRows + " * from [" + this.FileNevCSV.Trim() + "]";
            }

            //Creates the data adapter
            OdbcDataAdapter obj_oledb_da = new OdbcDataAdapter(sql_select, conn);

            //Fills dataset with the records from CSV file
            obj_oledb_da.Fill(ds, "csv");

            //closes the connection
            conn.Close();

        return ds;
    }

And setting the dataGridView’s data source like to

    // loads the first 500 rows from CSV file
this.dataGridView_preView.DataSource = LoadCSV(500);
this.dataGridView_preView.DataMember = "csv";

i, get this in the datagridview, i get one column yet i expect to see the data returned as 7 columns.

Plus, i have no idea where F2 and F3 columns are coming from

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:09:07+00:00Added an answer on June 8, 2026 at 10:09 pm

    I would probably do this a different way. I would use a StreamReader, and read in the file line by line, break the string up into object properties, and store the objects in a list. Then you bind the list to the datagridviews datasource. I demonstrate two quick ways to do this.

    1 -Tab separated data

    If the file is tab separated, as it seems to be, split the line into an array and assign each index with to a property like so.

    public partial class Form1 : Form
    {
        private void Form1_Load(object sender, EventArgs e)
        {
            var rows = new List<Row>();
            var sr = new StreamReader(@"C:\so_test.txt");
            while (!sr.EndOfStream)
            {
                string s = sr.ReadLine();
                if (!String.IsNullOrEmpty(s.Trim()))
                {
                    rows.Add(new Row(s));
                }
            }
            sr.Close();
            dataGridView1.DataSource = rows;
        }
    }
    
    public class Row
    {
        public double Number1 { get; set; }
        public double Number2 { get; set; }
        public double Number3 { get; set; }
        public double Number4 { get; set; }
        public double Number5 { get; set; }
        public double Number6 { get; set; }
        public double Number7 { get; set; }
        public string Date1 { get; set; }
    
        public Row(string str)
        {
            string[] separator = { "\t" };
            var arr = str.Split(separator, StringSplitOptions.None);
            Number1 = Convert.ToDouble(arr[0]);
            Number2 = Convert.ToDouble(arr[1]);
            Number3 = Convert.ToDouble(arr[2]);
            Number4 = Convert.ToDouble(arr[3]);
            Number5 = Convert.ToDouble(arr[4]);
            Number6 = Convert.ToDouble(arr[5]);
            Number7 = Convert.ToDouble(arr[6]);
            Date1 = arr[7];
        }
    }
    

    2 -Hard Start points and lengths

    If the data is tab separated, but conforms to strict start and endpoints for each column, you could declare the startpoints and lengths for each column as constants and get those via substring. This would only need a change in code in your Row class, like this. I have left of the constants from brevity, and just hardcoded them.

        public Row(string str)
        {
            Number1 = Convert.ToDouble(str.Substring(4, 6));
            Number2 = Convert.ToDouble(str.Substring(16, 6));
            Number3 = Convert.ToDouble(str.Substring(28, 7));
            Number4 = Convert.ToDouble(str.Substring(40, 7));
            Number5 = Convert.ToDouble(str.Substring(52, 6));
            Number6 = Convert.ToDouble(str.Substring(64, 6));
            Number7 = Convert.ToDouble(str.Substring(76, 6));
            Date1 = str.Substring(88, 24);
        }
    

    Screenshot

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a txt file, here is some sample data: Id,PriceOne,Company,PriceTwo,PriceThree 11,15.3599997,Japan ltd,12.23,3.1777777 12,,Koyoto
1) I have a text file below like this. Dilantha code 65 po Bo
Below my sample html file: some text here <img src=http://site.com/7b399e20/77165/5fa/2a31ffb8.jpg/> sometext here some text
I am using the below code snippet to FTP simple text files from a
I have below sample data. AID Date Title ----- ---------- ------ 1 2011-12-12 test1
I've put together a small code-sample below (Currently in C# 3.5 but would also
I have a IObservable [named rows in the sample below] from Reactive extensions framework
Below is some sample code. The window does not resize (python3.2, on a Mac,
In the sample code below I am dividing by zero which when I step
When I run the sample code below the width of JTextArea is fixed (100px)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.