title: Create editable powershell GUI for CSV data
body: <p>GOAL: Create a GUI form populated with CSV data, allow the user to edit the data, then save the data in an array for further manipulation.</p>
  
  <p>NOTE: Using PowerShell Studio to generate a form with data from the CSV</p>
  
  <p>CURRENT CODE: 
   - Calling code ($path is passed from the calling form):</p>
  
  <pre><code>$rows = Import-Csv -Path $path
  $table = ConvertTo-DataTable -InputObject $rows
  Load-DataGridView -DataGridView $datagridviewResults -Item $table
  </code></pre>
  
  <ul>
  <li>ConvertTo-DataTable function:</li>
  </ul>
  
  <pre><code>function ConvertTo-DataTable {
      
      [OutputType([System.Data.DataTable])]
      param(
          [ValidateNotNull()]
          $InputObject, 
          [ValidateNotNull()]
          [System.Data.DataTable]$Table,
          [switch]$RetainColumns,
          [switch]$FilterWMIProperties
      )
  
      if($Table -eq $null) {
          $Table = New-Object System.Data.DataTable
      }
  
      if($InputObject-is [System.Data.DataTable]) {
          $Table = $InputObject
      } else {
          if(-not $RetainColumns -or $Table.Columns.Count -eq 0) {
              #Clear out the Table Contents
              $Table.Clear()
  
              if($InputObject -eq $null){ return } #Empty Data
  
              $object = $null
              #find the first non null value
              foreach($item in $InputObject) {
                  if($item -ne $null) {
                      $object = $item
                      break   
                  }
              }
  
              if($object -eq $null) { return } #All null then empty
  
              #Get all the properties in order to create the columns
              foreach ($prop in $object.PSObject.Get_Properties()) {
                  if(-not $FilterWMIProperties -or -not $prop.Name.StartsWith('__')) { #filter out WMI properties
                      #Get the type from the Definition string
                      $type = $null
  
                      if($prop.Value -ne $null) {
                          try{ $type = $prop.Value.GetType() } catch {}
                      }
  
                      if($type -ne $null) { # -and [System.Type]::GetTypeCode($type) -ne 'Object')
                          [void]$table.Columns.Add($prop.Name, $type) 
                      } else { #Type info not found
                          [void]$table.Columns.Add($prop.Name)    
                      }
                  }
              }
  
              if($object -is [System.Data.DataRow]) {
                  foreach($item in $InputObject) {    
                      $Table.Rows.Add($item)
                  }
                  return  @(,$Table)
              }
          } else {
              $Table.Rows.Clear() 
          }
  
          foreach($item in $InputObject) {        
              $row = $table.NewRow()
  
              if($item) {
                  foreach ($prop in $item.PSObject.Get_Properties()) {
                      if($table.Columns.Contains($prop.Name)) {
                          $row.Item($prop.Name) = $prop.Value
                      }
                  }
              }
              [void]$table.Rows.Add($row)
          }
      }
  
      return @(,$Table)   
  }</code></pre>
  
  <ul>
  <li>Load-DataGridView function:</li>
  </ul>
  
  <pre><code>function Load-DataGridView {
      
      Param (
          [ValidateNotNull()]
          [Parameter(Mandatory=$true)]
          [System.Windows.Forms.DataGridView]$DataGridView,
          [ValidateNotNull()]
          [Parameter(Mandatory=$true)]
          $Item,
          [Parameter(Mandatory=$false)]
          [string]$DataMember
      )
      $DataGridView.SuspendLayout()
      $DataGridView.DataMember = $DataMember
      $DataGridView.EditMode = 'EditOnEnter'
  
      if ($Item -is [System.ComponentModel.IListSource]`
      -or $Item -is [System.ComponentModel.IBindingList]`
      -or $Item -is [System.ComponentModel.IBindingListView]) {
          $DataGridView.DataSource = $Item
      } else {
          $array = New-Object System.Collections.ArrayList
  
          if ($Item -is [System.Collections.IList]) {
              $array.AddRange($Item)
          } else {    
              $array.Add($Item)   
          }
          $DataGridView.DataSource = $array
      }
  
      $DataGridView.ResumeLayout()
  }</code></pre>
  
  <p>ADDITIONAL INFORMATION: Code is working in that it generates the Grid View and populates it with CSV data. However, I cannot edit it and need help coding the ability to capture changes once it is edited.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>12/5 EDIT</strong>: Added "$DataGridView.EditMode = 'EditOnEnter'" to the function "Load-DataGridView" above. Nothing changed. Tried to invoke the "BeginEdit" Event in a new RowCellClick event, but that didn't work either. Still struggling with this one.</p>