locked
Display datagrid from imported CSV file using powershell (Monad) RRS feed

  • Question

  • I'm attempting to display data from a csv file into a datagrid.  When I run the script it displays blank rows for each item that is in the csv file.

    Please advise, B

    Here's the code:

    $null=[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
    $null=[reflection.assembly]::LoadWithPartialName("System.Drawing")

    #Initialize DataGrid stuff
    $form = new-object System.Windows.Forms.Form
    $form.Size = new-object System.Drawing.Size 800,400
    $DataGrid = new-object System.windows.forms.DataGrid
    $DataGrid.AutoSize = $True

    $data = Import-CSV "C:\powershellScripts\test.csv"
    $DataGrid.DataSource =$data

    #finally show display the Grid
    $DataGrid.Dock = [System.Windows.Forms.DockStyle]::Fill
    $form.Controls.Add($DataGrid)
    $form.topmost = $true
    $null = $form.showdialog()

    Thursday, June 25, 2009 9:06 PM

Answers

  • Hi,

    The Import-CSV command returns a PSCustomObject  object, however, the DataGrid accepts a data source either of IList type or IListSource type. To bind the DataGrid, you can create a DataTable object and load data from the CSV file into this DataTable object.

    --------------------------------------------------------------------------------------------

    $null=[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
    $null=[reflection.assembly]::LoadWithPartialName("System.Drawing")
    $null=[reflection.assembly]::LoadWithPartialName("System.Data")

    #Initialize DataGrid stuff
    $form = new-object System.Windows.Forms.Form
    $form.Size = new-object System.Drawing.Size 800,400
    $DataGrid = new-object System.windows.forms.DataGrid
    $DataGrid.AutoSize = $True

    $data = Import-CSV "C:\test\test.csv"

    #Load data

    $path = "C:\test"
    $ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=$path;Extended properties='text;HDR=Yes;FMT=Delimited'"
    $Conn = new-object System.Data.OleDb.OleDbConnection($connString)
    $conn.open()
    $name = "test.csv"
    $cmd = new-object System.Data.OleDb.OleDbCommand("Select * from [$name]",$Conn)
    $da = new-object System.Data.OleDb.OleDbDataAdapter($cmd)
    $dt = new-object System.Data.dataTable
    [void]$da.fill($dt)

    $DataGrid.DataSource = $dt


    #finally show display the Grid
    $DataGrid.Dock = [System.Windows.Forms.DockStyle]::Fill
    $form.Controls.Add($DataGrid)
    $form.topmost = $true
    $null = $form.showdialog()

    --------------------------------------------------------------------------------------------

    Best Regards,
    Zhi-Xin







    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    • Proposed as answer by Zhi-Xin Ye Tuesday, June 30, 2009 7:23 AM
    • Marked as answer by Zhi-Xin Ye Wednesday, July 1, 2009 3:23 AM
    Friday, June 26, 2009 11:26 AM

All replies

  • Hi,

    The Import-CSV command returns a PSCustomObject  object, however, the DataGrid accepts a data source either of IList type or IListSource type. To bind the DataGrid, you can create a DataTable object and load data from the CSV file into this DataTable object.

    --------------------------------------------------------------------------------------------

    $null=[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
    $null=[reflection.assembly]::LoadWithPartialName("System.Drawing")
    $null=[reflection.assembly]::LoadWithPartialName("System.Data")

    #Initialize DataGrid stuff
    $form = new-object System.Windows.Forms.Form
    $form.Size = new-object System.Drawing.Size 800,400
    $DataGrid = new-object System.windows.forms.DataGrid
    $DataGrid.AutoSize = $True

    $data = Import-CSV "C:\test\test.csv"

    #Load data

    $path = "C:\test"
    $ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=$path;Extended properties='text;HDR=Yes;FMT=Delimited'"
    $Conn = new-object System.Data.OleDb.OleDbConnection($connString)
    $conn.open()
    $name = "test.csv"
    $cmd = new-object System.Data.OleDb.OleDbCommand("Select * from [$name]",$Conn)
    $da = new-object System.Data.OleDb.OleDbDataAdapter($cmd)
    $dt = new-object System.Data.dataTable
    [void]$da.fill($dt)

    $DataGrid.DataSource = $dt


    #finally show display the Grid
    $DataGrid.Dock = [System.Windows.Forms.DockStyle]::Fill
    $form.Controls.Add($DataGrid)
    $form.topmost = $true
    $null = $form.showdialog()

    --------------------------------------------------------------------------------------------

    Best Regards,
    Zhi-Xin







    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
    • Proposed as answer by Zhi-Xin Ye Tuesday, June 30, 2009 7:23 AM
    • Marked as answer by Zhi-Xin Ye Wednesday, July 1, 2009 3:23 AM
    Friday, June 26, 2009 11:26 AM
  • I found an alternative to created a datatable object.  It seems a bit easier... are there any downfalls to this method?

    $array= new-object System.Collections.ArrayList
    $data=@(Import-CSV "C:\Scripts\Test.csv"| write-output)
    $array.AddRange($data)
    $DataGridView.DataSource = $array
    Wednesday, July 1, 2009 5:34 PM
  • Very old post but 

    You can remove the Write-output from $data=@(Import-CSV "C:\Scripts\Test.csv"| write-output)


    Cyreli

    Saturday, April 21, 2012 6:06 PM