Click here to Skip to main content
14,391,825 members
Rate this:
Please Sign up or sign in to vote.
See more:
I wanted to read text file in C#. Code works well but I notcied that my code was not able to read the decimals. For example, if I have 159.58, I got in GridView 15958 even I defined the columns in DataGridView as double. Any Idea?

What I have tried:

<pre>public void ImportData()

        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Title = "Import text file";
        ofd.InitialDirectory = @"C:\";
        ofd.Multiselect = true;
        ofd.Filter = "txt file (*.txt)|*.txt| all files (*.*)|*.*";

        try {
            if (ofd.ShowDialog() == DialogResult.OK)
                label1.Text = ofd.FileName;


            string[] lines = File.ReadAllLines(ofd.FileName);
            string[] value;
            int count = 0;
            for (int i = 0; i < lines.Length; i++)
                value = lines[i].ToString().Split(',');
                string[] rows = new string[value.Length];

                for (int j = 0; j < value.Length; j++)
                    rows[j] = value[j].Trim();
                    count ++;

            MessageBox.Show("The file has been read from" + label1.Text+". It has"+" "+count/3+"lines");
        catch( Exception ex)

Updated 5-Nov-19 5:06am
Maciej Los 30-Oct-19 5:07am
And your text file structure is...
MadMyche 30-Oct-19 7:17am
Post a snippet of the text file itself, so that we can what type of "text" it really is
EngAb1989 30-Oct-19 7:21am
Have you any idea how can I put snapshot as attachments here in code project website?
EngAb1989 30-Oct-19 7:22am
How can I attach?
Rate this:
Please Sign up or sign in to vote.

Solution 2

It could be a cultural thing, see: c# 4.0 - how to set default culture info for entire c# application - Stack Overflow[^]
As you probably are using a German Windows version, comma's are expected for the decimals.

It could also be that you have comma's for the decimals in your text file, the Split(',') will then truncate them.
It would be better to use ';' as a separator (or TAB if you prefer that) and then use Split(';').
EngAb1989 5-Nov-19 10:14am
I change the current thread to English because you are right, I have Deutsch thread. But the problem is still. maybe can look at my code
Rate this:
Please Sign up or sign in to vote.

Solution 1

It's going to depend on other code: specifically the code you use to read the actual text file into your app - and that will depend on how the data in your text file is organised.

"Text files" have no "fixed format" - you can stick anything you like in there, it doesn't even have to contain just human readable data (or even human readable data!) For example, CSV data is text, XML data is text, JSON data is text. So is "one line per item", Base64, and INI - but you can't just "read the text file" and have teh actual data elements understandable in your app.

So start by looking at the input file using a text editor - notepad will probably do - and identify what kind of data it contains.
Then look at the code which reads that file, and check exactly what it does with it.

If it all looks reasonable, then use the debugger to look at exactly what it reads in, and exactly what it does with it.

Sorry, but we can't do any of that for you - we have no access to your app, or the data you are feeding it from!
EngAb1989 5-Nov-19 10:02am
I have text file (*.txt) the data is shown like below:

The part of my code for reading the text file is attached
OriginalGriff 5-Nov-19 10:07am
And? What does the debugger show you?
0x01AA 5-Nov-19 13:57pm
It looks your decimals do use a fix decimal separator (not culture dependend). So use double.Parse("3.5", CultureInfo.InvariantCulture) and more better the double.TryParse

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100