0

Note: in converting my long untidy code into pseudo-code I have not explained that I want to return an array/list that contains the data that as been loaded into arFiles. That is what line... // look at the contents of each file and store selected information in

means.

I have simple form with a button to load a directory of files and scan them and load an array with the selected data at runtime. How do I load the dataGridView1 with the contents of an array?

I have searched the web and cannot find a method to do it at runtime. The examples I have seen assume one is loading static data.

Please be gentle with me as I have not coded for approx 10 years.

private void btnReadLogFiles_Click(object sender, EventArgs e)
{
    string[,] arFiles;
    arFiles = this.getFileInfo();
    // watch window shows arFiles is loaded correctly

    // how do I load a dataGridView1 with the data from the array at runtime?
}

private string[,] getFileInfo()
{
    string[] oFiles = Directory.GetFiles(sPath, "*.csv");
    nColumns = 4;
    nRows = Directory.GetFiles(sPath, "*.csv").Length;

    // create an array of rows that matches the files in the directory
    string[,] arFiles = new string[nRows, 4];

    // look at the contents of each file and store selected information in arFiles.

    return arFiles;
}
Hunt
  • 279
  • 1
  • 3
  • 13
  • You can first [convert your 2d array `[,]` to a jagged array`[][]`](http://stackoverflow.com/questions/21986909/convert-multidimensional-array-to-jagged-array-in-c-sharp) then you can [bind `DataGridView` to jagged array `[][]`](https://stackoverflow.com/questions/38236282/how-to-bind-object-into-datagridview) – Reza Aghaei Dec 11 '16 at 14:49

2 Answers2

0

Unfortunately if you just assign an array of strings into DataGridView the result might not be the one you expect. Therefore, you can workaround this via converting the array of strings into list of anonymous objects and then assign it to the DataGridView

Example:

private void button1_Click(object sender, EventArgs e)
{
    this.dataGridView1.DataSource = GetFileInfo();
}

private List<object> GetFileInfo()
{
    string[] allPaths = Directory.GetFiles(@"C:\Program Files (x86)\Microsoft Visual Studio 10.0", "*.txt", SearchOption.AllDirectories);
    List<object> list = new List<object>();

    foreach (var path in allPaths)
    {
        // Create a new anonymous object
        list.Add(new { File = Path.GetFileName(path) });
    }

    return list;
}

Result:

enter image description here

ivayle
  • 1,070
  • 10
  • 17
  • Because I used pseudo code I have not made my question clear. I have created an array arFiles of the correct length and then processed the contents of each file in oFiles and loaded my findings string data into arFiles. It seems that I must somehow convert this to a List and pass that back to the form to load the grid. – Hunt Dec 12 '16 at 13:13
  • Ivaylo Petrov, since that returns one column and I need 4 including the file name could I add the 3 extra string columns I need. Then scan/loop through the List object (you created) adding the extra string data from each file I am opening/interrogating/collating in the List object? This then would replace my array that I have created in error. – Hunt Dec 12 '16 at 19:24
  • The main benefit of using anonymous object is that you can easily extend it. For example: `list.Add(new { File = Path.GetFileName(path), Length = path.Length });` and as a result File and Length will be associated with two columns of `DataGridView`. You can add as many as you would need columns that way. – ivayle Dec 12 '16 at 19:36
  • if someone can provide an example of using an array that allows provides the functionality of creating an array who's rows are designated at run time and who's count of rows is stated at design time and is empty. Then allows the resultant array to be scanned and the missing data be set by something like SetValue that would make loading datagridview possible. please see sample code https://stackoverflow.com/questions/41139456/load-a-list-from-an-array#41139456 – Hunt Dec 15 '16 at 13:37
0

The following code will put the files into 4 columns

       private List<List<string>> getFileInfo()
        {
            List<List<string>> arFiles = new List<List<string>>();

            string[] oFiles = Directory.GetFiles(sPath, "*.csv");
            nColumns = 4;

            List<string> newRow = null;
            for(int index = 0; index < oFiles.Count(); index++)
            {
                if ((index % nColumns) == 0)
                {
                    newRow = new List<string>();
                    arFiles.Add(newRow);
                }
                newRow.Add(oFiles[index]);
            }

            return arFiles;
        }
jdweng
  • 33,250
  • 2
  • 15
  • 20