Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to C#, but not programming. I have been challenged to read in a .dat file that has a specific record length, but no end of record or field delimiters. The file will not be edited, but should be sortable on each field. The file loads when form opens so it's the first thing the user sees.

What I have tried:

I chose to try the datagrid view.   Also, I've never made a datagrid view before.  I opted to play with the dat file using a sub-string initially.

First, I read the sub-strings into a record array, then tried to add the array with a loop.  This works beautifully - for the first record.  When I loop, I get nothing but a nice heavy bit of processing.

Next, I created a CSV separated line using the substrings, then split it to add to the table.  Same thing. Works great for first record - loop and more than 4 minutes pass (approx. 1500 records in dat file)

Should I approach it with a data connection? and if so, could you provide me with a link to where I can research it? I learn better by doing and playing with examples :)


private void Form1_Load(object sender, EventArgs e)
       {
           string inFile = "OH-PSF.DAT";

           //string[] record = new string[2000];
           string strLine;
           using (System.IO.StreamReader sr = new System.IO.StreamReader(inFile))
           {
               string filetext = sr.ReadToEnd(); //read in the file

               //int rowCount = 0;
               for (int x = 0; x < filetext.Length; x++)
               {
              // while (x < 600) {

                   string data1 = filetext.Substring(x, 9); x = 9;//next=index where next ss should start
                   string data2 = filetext.Substring(x, 10); x = x + 10; //10=currently read field size
                   string data3 = filetext.Substring(x, 2); x = x + 2;
                   string data4 = filetext.Substring(x, 28); x = x + 28;
                   string data5 = filetext.Substring(x, 20); x = x + 20;
                   string data6 = filetext.Substring(x, 2); x = x + 2;
                   string data7 = filetext.Substring(x, 20); x = x + 20;
                   string data8 = filetext.Substring(x, 6); x = x + 6;
                   string data9 = filetext.Substring(x, 7); x = x + 7;
                   strLine= data1 + "," + data2 + "," + data3 + "," + data4 + "," + data5 + "," + data6 + "," + data7 + "," + data8 + "," + data9;
                   string[] strArray = strLine.Split(',');
                   strArray = strLine.Split(',');
                   dataGridView1.Rows.Add(strArray);


                   //dataGridView1.Rows.Add(record[0], record[1], record[2], record[3], record[4], record[5], record[6], record[7], record[8]);
               }


           }


       }//ends form 1 load
Posted
Updated 26-Sep-19 6:13am

A couple of things.
1) Don't add rows to the DGV one by one - add them all at the end with with AddRange instead. Adding them one-by-one updates the display control each time, and that's slow.
2) Look at your code.
strLine= data1 + "," + data2 + "," + data3 + "," + data4 + "," + data5 + "," + data6 + "," + data7 + "," + data8 + "," + data9;
string[] strArray = strLine.Split(',');
strArray = strLine.Split(',');
Why?
Take individual values - all strings - assemble them into one string with an added separator, then split it up into separate strings based on the seperator you just added ... Does that seem like a sensible way to do things?
 
Share this answer
 
 
Share this answer
 
Thank you so much.. I feel silly. The record length was 104, and I was only increasing the loop by 1. That was problem 1. The splitting the delimited string was problem to, but the one that really fixed is was creating the data table, which I didn't realize I needed to do :) Thank you! I have learned a lot!

C#
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("data", typeof(string));
dt.Columns.Add("1");
dt.Columns.Add("2");
dt.Columns.Add("3");
dt.Columns.Add("4");
dt.Columns.Add("5");
dt.Columns.Add("6");
dt.Columns.Add("7");
dt.Columns.Add("8");
dt.Columns.Add("9");

ArrayList recordList = new ArrayList();

    using (System.IO.StreamReader sr = new System.IO.StreamReader(inFile))
    {
        string filetext = sr.ReadToEnd(); //read in the file


   for (int y = 0; y < filetext.Length; y+=104) //record length is 104 *facepalm* took 2 hours to figure this out!!
    {



        string[] strArray = new string[9];


        strArray[0] = filetext.Substring(x, 9); x = 9;//next=index where next ss should start
        strArray[1] = filetext.Substring(x, 10); x = x + 10; //10=currently read field size
        strArray[2] = filetext.Substring(x, 2); x = x + 2;
        strArray[3] = filetext.Substring(x, 28); x = x + 28;
        strArray[4] = filetext.Substring(x, 20); x = x + 20;
        strArray[5] = filetext.Substring(x, 2); x = x + 2;
        strArray[6] = filetext.Substring(x, 20); x = x + 20;
        strArray[7] = filetext.Substring(x, 6); x = x + 6;
        strArray[8] = filetext.Substring(x, 7); x = x + 7;
        recordList.Add(strArray);

        dt.Rows.Add(strArray);
    }
    dataGridView1.DataSource = dt;
 
Share this answer
 
v2
Comments
OriginalGriff 26-Sep-19 12:19pm    
The way I would do it - and you are free to ignore this if you want - would be to create a class that contained string properties for each field, and had a constructor which took the input data and a start offset. The constructor fills the strings in, and your main code then creates a List of the class instances. You can use the List directly as the DataSource, and the property names become the column names in your DGV automatically.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900