cellenter and cellleave not working

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

cellenter and cellleave not working

Post by supportMIB »

I am trying to get this to work...it is pulling some info from a database and placing it into a datagridview.

I have some things I want it to update when moving up and down the rows in the datagridview, but I cant seem to get it to work. I added a simple messagebox prompt and it is not working.

What am I doing wrong?
  1. $form1_Load={
  2.     $old = (get-date).adddays(-5)
  3.     $new = get-date
  4.     $selected = "vmibex-wr.bancshares.mib"
  5.     $connectionstring = "Server = devserver1; Database = events_data; Integrated Security = True"
  6.     $connection = New-Object System.Data.SqlClient.SqlConnection
  7.     $connection.ConnectionString = $connectionstring
  8.     $connection.Open()
  9.  
  10.     $query2 = "select id, count(*) as instances from events where machinename = '$selected' and timecreated >='$old' and timecreated <= '$new' group by id order by instances DESC"
  11.  
  12.     $command = $connection.CreateCommand()
  13.     $command.CommandText = $query2
  14.    
  15.     $result = $command.ExecuteReader()
  16.    
  17.     $table2 = new-object System.Data.DataTable
  18.     $table2.Load($result)
  19.    
  20.     $connection.Close()
  21.    
  22.     $datagridview1.DataSource = $table2
  23. }
  24.  
  25. #region Control Helper Functions
  26. function Load-DataGridView
  27. {
  28.     <#
  29.     .SYNOPSIS
  30.         This functions helps you load items into a DataGridView.
  31.  
  32.     .DESCRIPTION
  33.         Use this function to dynamically load items into the DataGridView control.
  34.  
  35.     .PARAMETER  DataGridView
  36.         The DataGridView control you want to add items to.
  37.  
  38.     .PARAMETER  Item
  39.         The object or objects you wish to load into the DataGridView's items collection.
  40.    
  41.     .PARAMETER  DataMember
  42.         Sets the name of the list or table in the data source for which the DataGridView is displaying data.
  43.  
  44.     .PARAMETER AutoSizeColumns
  45.         Resizes DataGridView control's columns after loading the items.
  46.     #>
  47.     Param (
  48.         [ValidateNotNull()]
  49.         [Parameter(Mandatory=$true)]
  50.         [System.Windows.Forms.DataGridView]$DataGridView,
  51.         [ValidateNotNull()]
  52.         [Parameter(Mandatory=$true)]
  53.         $Item,
  54.         [Parameter(Mandatory=$false)]
  55.         [string]$DataMember,
  56.         [System.Windows.Forms.DataGridViewAutoSizeColumnMode]$AutoSizeColumns = 'None'
  57.     )
  58.     $DataGridView.SuspendLayout()
  59.     $DataGridView.DataMember = $DataMember
  60.    
  61.     if ($Item -is [System.Data.DataSet] -and $Item.Tables.Count -gt 0)
  62.     {
  63.         $DataGridView.DataSource = $Item.Tables[0]
  64.     }
  65.     elseif ($Item -is [System.ComponentModel.IListSource]`
  66.     -or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView] )
  67.     {
  68.         $DataGridView.DataSource = $Item
  69.     }
  70.     else
  71.     {
  72.         $array = New-Object System.Collections.ArrayList
  73.        
  74.         if ($Item -is [System.Collections.IList])
  75.         {
  76.             $array.AddRange($Item)
  77.         }
  78.         else
  79.         {
  80.             $array.Add($Item)
  81.         }
  82.         $DataGridView.DataSource = $array
  83.     }
  84.    
  85.     if ($AutoSizeColumns -ne 'None')
  86.     {
  87.         $DataGridView.AutoResizeColumns($AutoSizeColumns)
  88.     }
  89.    
  90.     $DataGridView.ResumeLayout()
  91. }
  92.  
  93. function ConvertTo-DataTable
  94. {
  95.     <#
  96.         .SYNOPSIS
  97.             Converts objects into a DataTable.
  98.    
  99.         .DESCRIPTION
  100.             Converts objects into a DataTable, which are used for DataBinding.
  101.    
  102.         .PARAMETER  InputObject
  103.             The input to convert into a DataTable.
  104.    
  105.         .PARAMETER  Table
  106.             The DataTable you wish to load the input into.
  107.    
  108.         .PARAMETER RetainColumns
  109.             This switch tells the function to keep the DataTable's existing columns.
  110.        
  111.         .PARAMETER FilterWMIProperties
  112.             This switch removes WMI properties that start with an underline.
  113.    
  114.         .EXAMPLE
  115.             $DataTable = ConvertTo-DataTable -InputObject (Get-Process)
  116.     #>
  117.     [OutputType([System.Data.DataTable])]
  118.     param(
  119.     [ValidateNotNull()]
  120.     $InputObject,
  121.     [ValidateNotNull()]
  122.     [System.Data.DataTable]$Table,
  123.     [switch]$RetainColumns,
  124.     [switch]$FilterWMIProperties)
  125.    
  126.     if($null -eq $Table)
  127.     {
  128.         $Table = New-Object System.Data.DataTable
  129.     }
  130.    
  131.     if ($InputObject -is [System.Data.DataTable])
  132.     {
  133.         $Table = $InputObject
  134.     }
  135.     elseif ($InputObject -is [System.Data.DataSet] -and $InputObject.Tables.Count -gt 0)
  136.     {
  137.         $Table = $InputObject.Tables[0]
  138.     }
  139.     else
  140.     {
  141.         if (-not $RetainColumns -or $Table.Columns.Count -eq 0)
  142.         {
  143.             #Clear out the Table Contents
  144.             $Table.Clear()
  145.            
  146.             if ($null -eq $InputObject) { return } #Empty Data
  147.            
  148.             $object = $null
  149.             #find the first non null value
  150.             foreach ($item in $InputObject)
  151.             {
  152.                 if ($null -ne $item)
  153.                 {
  154.                     $object = $item
  155.                     break
  156.                 }
  157.             }
  158.            
  159.             if ($null -eq $object) { return } #All null then empty
  160.            
  161.             #Get all the properties in order to create the columns
  162.             foreach ($prop in $object.PSObject.Get_Properties())
  163.             {
  164.                 if (-not $FilterWMIProperties -or -not $prop.Name.StartsWith('__')) #filter out WMI properties
  165.                 {
  166.                     #Get the type from the Definition string
  167.                     $type = $null
  168.                    
  169.                     if ($null -ne $prop.Value)
  170.                     {
  171.                         try { $type = $prop.Value.GetType() }
  172.                         catch { }
  173.                     }
  174.                    
  175.                     if ($null -ne $type) # -and [System.Type]::GetTypeCode($type) -ne 'Object')
  176.                     {
  177.                         [void]$table.Columns.Add($prop.Name, $type)
  178.                     }
  179.                     else #Type info not found
  180.                     {
  181.                         [void]$table.Columns.Add($prop.Name)
  182.                     }
  183.                 }
  184.             }
  185.            
  186.             if ($object -is [System.Data.DataRow])
  187.             {
  188.                 foreach ($item in $InputObject)
  189.                 {
  190.                     $Table.Rows.Add($item)
  191.                 }
  192.                 return @( ,$Table)
  193.             }
  194.         }
  195.         else
  196.         {
  197.             $Table.Rows.Clear()
  198.         }
  199.        
  200.         foreach ($item in $InputObject)
  201.         {
  202.             $row = $table.NewRow()
  203.            
  204.             if ($item)
  205.             {
  206.                 foreach ($prop in $item.PSObject.Get_Properties())
  207.                 {
  208.                     if ($table.Columns.Contains($prop.Name))
  209.                     {
  210.                         $row.Item($prop.Name) = $prop.Value
  211.                     }
  212.                 }
  213.             }
  214.             [void]$table.Rows.Add($row)
  215.         }
  216.     }
  217.    
  218.     return @(,$Table)  
  219. }
  220. #endregion
  221.  
  222. $datagridview1_cellenter = [System.Windows.Forms.DataGridViewCellEventHandler]{
  223.     Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  224.     [System.Windows.Forms.messagebox]::show("enter")
  225. }
  226. $datagridview1_cellleave = [System.Windows.Forms.DataGridViewCellEventHandler]{
  227.     Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
  228.     [System.Windows.Forms.messagebox]::show("leave")
  229. }
  230. [/code][codebox=powershell file=Untitled.ps1]
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: cellenter and cellleave not working

Post by jvierra »

Please attach your PFF fil. It is the only rational way to look at forms code.

Add the file as an attachment.
User avatar
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Re: cellenter and cellleave not working

Post by supportMIB »

Attached the project

Thanks again for your help!
Attachments
test.psf
(27.93 KiB) Downloaded 131 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: cellenter and cellleave not working

Post by jvierra »

Try this and look closely to see how it works.

Be sure "Output" pane is showing to see the diagnostics.

I would suggest spending some time with the manual on PSS as it will help you to avoid fundamental errors.
Attachments
test.psf
(18.88 KiB) Downloaded 128 times
User avatar
supportMIB
Posts: 62
Last visit: Thu Feb 29, 2024 11:17 am

Re: cellenter and cellleave not working

Post by supportMIB »

That really was a valuable lesson...really.

I see you added the events cellleave and cellenter. Thank you again for making me figure that out.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: cellenter and cellleave not working

Post by jvierra »

There are many, many things like that in PSS. It is worth reading the manual. It is short but very useful.
This topic is 7 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked