0

I am trying to write the contents of a text file to a datagridview. If I iterate through the array lines, I see the strings. When I add the array to a datagridview, I see properties of the file, line numbers, and string lengths -- but not the strings, which is all I want.

$LogFileRaw = Get-Content($selected)

If I use .Net, I just see the string lengths

$LogFileRaw = [System.IO.File]::ReadAllLines($selected)

I think that I haven't correctly bound the string array to a DataGridView control. I've found examples of doing this in other languages, but not in Powershell. Can someone can point me to a Powershell example or explain what I'm doing wrong? Thank you for your patience.

1 Answer 1

1

You can use Get-Content, but you need to give your log file content 'column' a name:

$LogFileRaw = Get-Content($selected) | % {
   New-Object PSObject -Property @{
      "Log Entry" = $_
   }
}

Then you can add this to the datagridview:

$array = New-Object System.Collections.ArrayList
$array.AddRange($LogFileRaw)

$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.DataSource = $array

This is much simpler when importing a .csv file because you can use the -header parameter of ConvertFrom-Csv.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. What an elegant solution. I'm a newb at PS. This REALLY helped me out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.