1

I have the following data that I wish to import into a DataGridView:

01-29-15  04:04AM            505758360 examplefilename1.zip
01-28-15  12:28AM            501657000 this_is_another_file.zip
01-29-15  02:27AM           1629952132 random.data.for.example.zip

This data isn't delimited by and particular number of characters or by any characters. I need to import this data into a DataGridView, I have the following code:

public void LoadDataToGrid(string pProfile)
{
    string[] lvTextData = File.ReadAllLines(Global.lvPath + @"\" + pProfile + ".txt");

    DataTable dtTextData = new DataTable();

    dtTextData.Columns.Add("Company Name", typeof(string));
    dtTextData.Columns.Add("Size", typeof(string));
    dtTextData.Columns.Add("File Name", typeof(string));
    dtTextData.Columns.Add("Last Upload", typeof(string));

    for(int i=1; i < lvTextData.Length; i++)
        dtTextData.Rows.Add(lvTextData[i].Split());

    grdData.DataSource = dtTextData;
}

The data comes in fine but only sits in one column, how can I change define the column widths?

  • your sample data seems to be missing some fields (company name and machine code). Is the input data inconsistent (i.e. sometimes missing fields), or is it always in the format: `last upload / size / file name`? – Rufus L Jan 29 '15 at 20:33
  • Sorry, I shouldn't have left them in, they will be populated by other methods. The data is purely "Date/Time", "Bytes", and "Filename". –  Jan 30 '15 at 09:23

2 Answers2

0

There seem to be a few problems with your code (and the data you provide):

when you split the string

01-29-15  04:04AM            505758360 examplefilename1.zip

It splits it into an array of strings of Length == 16 (cause it splits all the whitespace characters). But you're providing only 4 Columns. So you want to put an array of 16 strings into 4 columns, which clearly can't be done.

A simple thing to do is: Remove redundant whitespaces of your input string Regex.Replace(s, "\\s+, " ");. (you also could Regex-parse the string and split it into groups). Then you can split your string by whitespaces and you'll get an array of strings of Length == 4

For your example (although your input data obviously is not corresponding with the names of your columns):

 for (int i = 1; i < lvTextData.Length; i++)
 {
     // removes redundant whitespaces
     string removedWhitespaces = Regex.Replace(lvTextData[i], "\\s+", " ");
     // splits the string
     string[] splitArray = removedWhitespaces.Split(' ');
     // [0]: 01-29-15
     // [1]: 04:04AM
     // [2]: 505758360
     // [3]: examplefilename1.zip

     // do some kind of length checking here
     if(splitArray.Length == dtTextData.Columns.Count) 
     { 
         dtTextData.Rows.Add(splitArray);
     }
 }
stefankmitph
  • 3,236
  • 2
  • 18
  • 22
0

You can even look for CSV Reader - if you are using NuGet look here

It handles also trailing / ending white spaces automatically. Keep in mind that you have to specify '\t' or ' ' as delimiting character.

void ReadAndBindCsv()
{
   // open the file "data.csv" which is a CSV file with headers
   using (CsvReader csv = new CsvReader(
                       new StreamReader("data.csv"), true))
   {
        csv.TrimSpaces = true;
        grdData.DataSource = csv;
   }
}
nhaberl
  • 417
  • 1
  • 4
  • 17