EventData not working as intended within conditions

Product: PowerShell Universal
Version: 5.1.0

Heya I am currently trying to tidy up my powershell universal pages. I use a data grid to display my data, before now I just inserted every grid column manually but I am trying to make that more efficient.
So I tried using this method with Get-Member and a for loop

  $SQLSpalten += ($DATA | Get-Member | Where-Object { $_.MemberType -eq "Property" }).Name
            New-UDDataGrid -LoadRows {
     @{
         rows = [Array]$Rows | Select-Object $SQLSpalten
         rowCount = $Rows.count
     }
     
 } -Columns @(
     Foreach ($prop in $SQLSpalten)
     { 
        if ($prop -eq 'Placeholder1' -or $prop -eq 'Placeholder2') {
                New-UDDataGridColumn -Field $prop -Title $prop -Width 250 -Render {
                    New-UDLink -Url "https://$($EventData.$prop)" -Text $EventData.$prop -OpenInNewWindow -Style @{
                    color = '#0066cc' 
                    }
                }

            
        } elseif ($prop -eq 'Placeholder3' -or $prop -eq 'Placeholder4') {
            New-UDDataGridColumn -Field $prop -Title $prop -Width 200 -Render {
                if ($EventData.$prop -eq 'on' -or $EventData.$prop -eq 'ok') {
                New-UDAlert -Severity 'Success' -Text $EventData.$prop
                }
                else {
                New-UDAlert -Severity 'Error' -Text $EventData.$prop
                }
                }
        } else {
            New-UDDataGridColumn -Field $prop -Title $prop -Width 200
        }

        
     }
    ) -AutoHeight $true -PageSize 50 -ShowPagination -RowsPerPageOptions @(25, 50, 100, 500)
}

The issue I am facing is that the conditions with the or operator for some reason every single one of them just takes the value of placeholder2 even the data in the elseif block. I was wondering if anyone had any insight on this? trying to debug EventData in the console does not give me much of a return.

Thank you in advance for reading my post.

Hey! Welcome to the forums!

Initially this seemed like an easier problem to parse than it ended up being. I’ll start with just showing you I’m accomplishing a similar (if simpler) case.

I have a datagrid with dynamic columns that I load like this:

# Get the columns as a raw NoteProperty[]
        $DGPreColumns = $InputData | Get-Member -Type NoteProperty | Select-Object -ExpandProperty Name
 # Create the actual column objects
        $DGColumns = $DGPreColumns.forEach({
            New-UDDataGridColumn -Field $_
        })

Then, it’s splatted into New-UDDataGrid like so:

# Define a hashtable of properties to pass to the table
        $DGProperties = @{
            AutoHeight = $true;
            Columns = $DGColumns;
            Id = $Id;
            LoadRows = { Out-UDDataGridData -Data $InputData -Context $EventData -TotalRows $Rows.Length };
            ShowPagination = $True;
            StripedRows = $True
        }

        # If we have some detail content, add it to the properties array
        if ($DetailContent) {
            $DGProperties.LoadDetailContent = $DetailContent
        }

        New-UDDataGrid @DGProperties

The main difference I’m seeing is that your -LoadRows never utilizes Out-UDDataGridData. However, I’m not sure what your issue is:

  1. Is every $prop evaluated as if it were Placeholder2?
  2. Is the data for each row only pulling from the Placeholder2 column (resulting in the same piece of information duplicated across each column for a given row)?
  3. Is every column just Placeholder2, with the condition evaluating “correctly” as if it were Placeholder2?

Sorry if I’m not much help - let me know if there are any more specifics you can provide.
:wavy_dash:ZG

1 Like

Thank you for your welcome! No please I appreciate all the help and time

it’s specifically the $prop in the if and elseif code blocks the final else that creates a simple grid column fetches the other data normally.

so it’s like prop takes on the value of placeholder2 and keeps it until it leaves those two code blocks.

Is the number of columns correct? Like, there are 4 Placeholder2 columns?

yes I pull data from a database with 16 columns and besides the 4columns that just hold data from placeholder2 everything is correct

Interesting… can you Write-Debug (ConvertTo-Json $SqlSpalten) and see what it’s returning? That’s where I would start. Without access to your database it’s hard to try and troubleshoot from my end…

thank you again for your help, I ended up simplifying my script in a way that I utilize Out-UDDataGridData and dealt with the headache that way

1 Like