Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Powershell字符串数组到datagridview_String_Datagridview_Powershell 3.0 - Fatal编程技术网

String Powershell字符串数组到datagridview

String Powershell字符串数组到datagridview,string,datagridview,powershell-3.0,String,Datagridview,Powershell 3.0,我正在尝试将文本文件的内容写入datagridview。如果我遍历数组行,我会看到字符串。当我将数组添加到datagridview时,我会看到文件的属性、行号和字符串长度——但不会看到字符串,这就是我想要的 $LogFileRaw = Get-Content($selected) 如果我使用.Net,我只看到字符串的长度 $LogFileRaw = [System.IO.File]::ReadAllLines($selected) 我认为我没有将字符串数组正确绑定到DataGridView控

我正在尝试将文本文件的内容写入datagridview。如果我遍历数组行,我会看到字符串。当我将数组添加到datagridview时,我会看到文件的属性、行号和字符串长度——但不会看到字符串,这就是我想要的

$LogFileRaw = Get-Content($selected)
如果我使用.Net,我只看到字符串的长度

$LogFileRaw = [System.IO.File]::ReadAllLines($selected)
我认为我没有将字符串数组正确绑定到DataGridView控件。我在其他语言中找到了这样做的例子,但在Powershell中没有。有人能给我举个Powershell的例子或解释我做错了什么吗?
感谢您的耐心。

您可以使用
获取内容
,但您需要为日志文件内容“列”指定一个名称:

$LogFileRaw = Get-Content($selected) | % {
   New-Object PSObject -Property @{
      "Log Entry" = $_
   }
}
然后可以将其添加到datagridview:

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

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

这在导入.csv文件时要简单得多,因为您可以使用
ConvertFrom csv的-header参数

非常感谢。多么优雅的解决方案。我是PS的新手,这真的帮了我大忙。