Csv + datagrid reload data

avatar
(anonymous user)
Product: PowerShell Universal 
Version: 5.5.5


Hello Everyone

I’m getting started with PSU, and overall, I find it very promising.

However, I am encountering some difficulties with what appear to be straightforward tasks.

Specifically, I want to load a CSV file into a datagrid. This works correctly the first time.

I also have a button to trigger some actions.

Afterward, I want to implement a reset button to bring the application back to its initial state. The issue I’m facing is that, after resetting, although I can upload the CSV file again, I must manually refresh the page (via F5 or a refresh button) for the datagrid to display the data properly.

Is there a way to avoid this manual page refresh? I attempted to use Sync-UDElement, but it doesn’t seem to have the desired effect.

Any advice or best practices to handle this situation would be greatly appreciated.

    # --- Upload CSV ---
    New-UDUpload -Text 'Upload CSV' -OnUpload {
        $base64 = $Body.data
        $bytes = [Convert]::FromBase64String($base64)
        $content = [System.Text.Encoding]::UTF8.GetString($bytes)
        $csvData = $content | ConvertFrom-Csv -Delimiter ';'

        # Ajouter un champ Id unique
        $csvDataWithId = $csvData | ForEach-Object -Begin { $i = 0 } -Process {
            $i++
            $_ | Add-Member -MemberType NoteProperty -Name Id -Value $i -PassThru
        }

        $Session:csvData = $csvDataWithId

        # refresh
        Sync-UDElement -Id 'csvDataGrid'
        Sync-UDElement -Id 'vcConnectZone'
    }  -Accept ".csv"

    # --- Bouton Reset ---
    New-UDButton -Text "Réinitialiser l'application" `
    -Style @{ backgroundColor = "#2196F3"; color = "white" } `
    -OnClick {
        if ($Session:VCConnected) {
            Disconnect-VIServer -Server $Session:SelectedVC -Confirm:$false
            $Session:VCConnected = $false
        }

        $Session:csvData = $null
        $Session:SelectedVC = $null
        $Session:VCUser = $null
        $Session:VCPass = $null
        $Session:VCConnLog = $null
        $Session:Jobs = @{}
        $Session:logs = @{}

        Sync-UDElement -Id 'csvDataGrid'
        Sync-UDElement -Id 'vcConnectZone'
        Sync-UDElement -Id 'launchZone'
        Sync-UDElement -Id 'logZone'
        Sync-UDElement -Id 'vcConnLogCard'
        Sync-UDElement -Id 'info'
    }

    # --- DataGrid ---
    New-UDDynamic -Id 'csvDataGrid' -Content {
        if (-not $Session:csvData -or $Session:csvData.Count -eq 0) {
            New-UDTypography -Text 'Aucun CSV chargé ou fichier vide.'
        }
        else {
            $columns = $Session:csvData[0].PSObject.Properties.Name | ForEach-Object {
                New-UDDataGridColumn -Field $_ -HeaderName $_ -Flex 1
            }

            New-UDDataGrid -Id 'csvDataGrid' -CheckboxSelection -Columns $columns -LoadRows {
                @{
                    rows = $Session:csvData
                    rowCount = $Session:csvData.Count
                }
            }
        }
    }


All Comments (3)

avatar

Looks like you need to add the $csvData = $content | ConvertFrom-Csv -Delimiter ‘;’ to the new-UDDynamic section so it will reload. And reload your $session variable $Session:csvData = $csvDataWithId

avatar

Hello @AnonymousUser i tried to add $csvData = $content | ConvertFrom-Csv -Delimiter ‘;’ and to reload $Session:csvData = $csvDataWithId to my New-UDDynamic -Id 'csvDataGrid' but is even worse, even the first time i didn’t show the data on the datagrid but with a manual refresh same result. in all the cases thank you for your answer

avatar

Ok looking again, it needs to be where you have it originally and also add to the -OnClick section for the button that does the refresh. It should re-import the data to the variable, then sync-udelement should show new info. good luck.