Logo Pastebin.fr
Pastebin

Retrouvez, créez et partagez vos snippets en temps réel.

detail et recherche

# ===============================
# GROUPE RECHERCHE
# ===============================
$grpSearch = New-Object System.Windows.Forms.GroupBox
$grpSearch.Text = "🔍 Recherche"
$grpSearch.Location = New-Object System.Drawing.Point(20,520)
$grpSearch.Size = New-Object System.Drawing.Size(1080,70)
$grpSearch.ForeColor = [System.Drawing.Color]::DeepSkyBlue
$grpSearch.BackColor = [System.Drawing.Color]::FromArgb(20,20,35)   # fond sombre futuriste
$grpSearch.Font = New-Object System.Drawing.Font("Segoe UI Semibold", 12, [System.Drawing.FontStyle]::Bold)
$grpSearch.Anchor = "Bottom,Left,Right"
$form.Controls.Add($grpSearch)

# TextBox futuriste avec glow
$tbSearch = New-Object System.Windows.Forms.TextBox
$tbSearch.Location = New-Object System.Drawing.Point(20,26)
$tbSearch.Size = New-Object System.Drawing.Size(450,28)
$tbSearch.BackColor = [System.Drawing.Color]::FromArgb(30,30,50)
$tbSearch.ForeColor = [System.Drawing.Color]::Lime
$tbSearch.Font = New-Object System.Drawing.Font("Consolas", 11, [System.Drawing.FontStyle]::Bold)
$tbSearch.BorderStyle = "FixedSingle"
$grpSearch.Controls.Add($tbSearch)

# Bouton Réinitialiser futuriste
$btnReset = New-Object System.Windows.Forms.Button
$btnReset.Text = "âŸČ"
$btnReset.Location = New-Object System.Drawing.Point(480,24)
$btnReset.Size = New-Object System.Drawing.Size(45,30)
$btnReset.BackColor = [System.Drawing.Color]::FromArgb(50,50,80)
$btnReset.ForeColor = [System.Drawing.Color]::Cyan
$btnReset.FlatStyle = "Flat"
$btnReset.Font = New-Object System.Drawing.Font("Segoe UI", 14, [System.Drawing.FontStyle]::Bold)
$grpSearch.Controls.Add($btnReset)

$form.Add_Shown({ Refresh-AllItems })

# Filtrage automatique sur toutes les colonnes
$tbSearch.Add_TextChanged({
    $searchText = $tbSearch.Text.ToLower()

    $list.BeginUpdate()
    $list.Items.Clear()

    $results = $allItems | Where-Object {
        $_.SubItems.Text.ToLower()  -match $searchText
    }

    if($results) { 
        foreach ($res in $results) { 
            $null = $list.Items.Add($res.Clone())
        }
    }

    $list.EndUpdate()
})

# Réinitialisation
$btnReset.Add_Click({
    $tbSearch.Clear()
    $list.Items.Clear()
    foreach ($i in $allItems) { $list.Items.Add($i.Clone()) | Out-Null }
})


########################################################################


function Show-CustomError($policeId) {
    $errorForm = New-Object System.Windows.Forms.Form
    $errorForm.Text = "⚠ Erreur"
    $errorForm.Size = New-Object System.Drawing.Size(400,200)
    $errorForm.StartPosition = "CenterParent"
    $errorForm.BackColor = [System.Drawing.Color]::FromArgb(25,25,40)
    $errorForm.Font = New-Object System.Drawing.Font("Segoe UI", 11)

    $lblTitle = New-Object System.Windows.Forms.Label
    $lblTitle.Text = "Client introuvable"
    $lblTitle.Font = New-Object System.Drawing.Font("Segoe UI Semibold", 14, [System.Drawing.FontStyle]::Bold)
    $lblTitle.ForeColor = [System.Drawing.Color]::White
    $lblTitle.Location = New-Object System.Drawing.Point(20,20)
    $lblTitle.AutoSize = $true
    $errorForm.Controls.Add($lblTitle)

    $lblMsg = New-Object System.Windows.Forms.Label
    $lblMsg.Text = "Aucun fichier client n’a Ă©tĂ© trouvĂ© pour la police $policeId"
    $lblMsg.Location = New-Object System.Drawing.Point(20,60)
    $lblMsg.Size = New-Object System.Drawing.Size(350,60)
    $lblMsg.ForeColor = [System.Drawing.Color]::White
    $errorForm.Controls.Add($lblMsg)

    $btnOk = New-Object System.Windows.Forms.Button
    $btnOk.Text = "OK"
    $btnOk.Location = New-Object System.Drawing.Point(280,120)
    $btnOk.Size = New-Object System.Drawing.Size(80,30)
    $btnOk.BackColor = [System.Drawing.Color]::FromArgb(70,20,20)
    $btnOk.ForeColor = [System.Drawing.Color]::White
    $btnOk.FlatStyle = "Flat"
    $btnOk.Font = New-Object System.Drawing.Font("Segoe UI", 10, [System.Drawing.FontStyle]::Bold)
    $btnOk.Add_Click({ $errorForm.Close() })
    $errorForm.Controls.Add($btnOk)

    $errorForm.ShowDialog()
}

$btnDetail.Add_Click({
    if ($list.SelectedItems.Count -gt 0) {
        $policeId = $list.SelectedItems[0].Text

        # Parcourir tous les fichiers CSV du dossier
        $rootPath   = "C:\Users\walid\Desktop\test\detail"
        $clientFiles = Get-ChildItem $rootPath -Filter *.csv
        $allData    = @()

        foreach ($file in $clientFiles) {
            $data  = Import-Csv $file.FullName
            $found = $data | Where-Object { $_.Contrat -eq $policeId -or $_.Police -eq $policeId }
            if ($found) {
                foreach ($row in $found) {
                    # On garde la trace du fichier source uniquement en mémoire
                    $row | Add-Member -NotePropertyName "SourceFile" -NotePropertyValue $file.FullName -Force
                    $allData += $row
                }
            }
        }

        if ($allData.Count -gt 0) {
            # DataTable d'affichage (sans colonne Source)
            $table = New-Object System.Data.DataTable
            [void]$table.Columns.Add("Champ")
            [void]$table.Columns.Add("Valeur")

            # Grouper par fichier et insérer un séparateur avant chaque groupe
            $grouped = $allData | Group-Object SourceFile
            foreach ($grp in $grouped) {
                $sepRow = $table.NewRow()
                $sepRow["Champ"]  = "*"
                $sepRow["Valeur"] = "📂 Fichier : " + [System.IO.Path]::GetFileNameWithoutExtension($grp.Name)
                $table.Rows.Add($sepRow)

                foreach ($clientData in $grp.Group) {
                    foreach ($prop in $clientData.PSObject.Properties) {
                        if ($prop.Name -ne "SourceFile" -and -not [string]::IsNullOrWhiteSpace($prop.Value)) {
                            $row = $table.NewRow()
                            $row["Champ"]  = $prop.Name
                            $row["Valeur"] = $prop.Value
                            $table.Rows.Add($row)
                        }
                    }
                }
            }

            # FenĂȘtre dashboard (glass)
            $detailForm = New-Object System.Windows.Forms.Form
            $detailForm.Text = "📊 Dashboard"
            $detailForm.Size = New-Object System.Drawing.Size(950,550)
            $detailForm.StartPosition = "CenterParent"
            $detailForm.Font = New-Object System.Drawing.Font("Segoe UI", 11)
            $detailForm.FormBorderStyle = "FixedDialog"
            $detailForm.MaximizeBox = $false
            $detailForm.MinimizeBox = $true
            $detailForm.BackColor = [System.Drawing.Color]::FromArgb(40,40,60)
            $detailForm.Opacity = 0.95

            # Titre
            $lblTitle = New-Object System.Windows.Forms.Label
            $lblTitle.Text = "✹ DonnĂ©es Client ($policeId)"
            $lblTitle.Font = New-Object System.Drawing.Font("Segoe UI Semibold", 20, [System.Drawing.FontStyle]::Bold)
            $lblTitle.ForeColor = [System.Drawing.Color]::DeepSkyBlue
            $lblTitle.Location = New-Object System.Drawing.Point(20,20)
            $lblTitle.AutoSize = $true
            $detailForm.Controls.Add($lblTitle)

            # DataGridView
            $grid = New-Object System.Windows.Forms.DataGridView
            $grid.Location = New-Object System.Drawing.Point(20,70)
            $grid.Size = New-Object System.Drawing.Size(880,380)
            $grid.ReadOnly = $false
            $grid.AllowUserToAddRows = $false
            $grid.AllowUserToDeleteRows = $false
            $grid.AutoSizeColumnsMode = "Fill"
            $grid.BackgroundColor = [System.Drawing.Color]::FromArgb(30,30,50)
            $grid.GridColor = [System.Drawing.Color]::MediumPurple
            $grid.MultiSelect = $true
            $grid.ClipboardCopyMode = "EnableAlwaysIncludeHeaderText"

            # Style des cellules
            $grid.DefaultCellStyle.BackColor = [System.Drawing.Color]::FromArgb(25,25,40)
            $grid.DefaultCellStyle.ForeColor = [System.Drawing.Color]::LimeGreen
            $grid.DefaultCellStyle.Font = New-Object System.Drawing.Font("Consolas", 11, [System.Drawing.FontStyle]::Bold)
            $grid.DefaultCellStyle.SelectionBackColor = [System.Drawing.Color]::MediumPurple
            $grid.DefaultCellStyle.SelectionForeColor = [System.Drawing.Color]::White
            $grid.DefaultCellStyle.Alignment = "MiddleCenter"

            # En-tĂȘtes
            $grid.ColumnHeadersDefaultCellStyle.BackColor = [System.Drawing.Color]::FromArgb(45,45,70)
            $grid.ColumnHeadersDefaultCellStyle.ForeColor = [System.Drawing.Color]::DeepSkyBlue
            $grid.ColumnHeadersDefaultCellStyle.Font = New-Object System.Drawing.Font("Segoe UI", 12, [System.Drawing.FontStyle]::Bold)
            $grid.EnableHeadersVisualStyles = $false

            # Charger le DataTable
            $grid.DataSource = $table

            # Style + protection des séparateurs
            $grid.Add_RowPrePaint({
                param($sender,$e)
                $row = $grid.Rows[$e.RowIndex]
                if ($row.Cells["Champ"].Value -eq "*") {
                    $row.DefaultCellStyle.BackColor = [System.Drawing.Color]::FromArgb(60,0,90)   # violet néon
                    $row.DefaultCellStyle.ForeColor = [System.Drawing.Color]::White
                    $row.DefaultCellStyle.Font = New-Object System.Drawing.Font("Segoe UI", 12, [System.Drawing.FontStyle]::Bold)
                    $row.ReadOnly = $true
                }
            })

            # EmpĂȘcher la sĂ©lection des sĂ©parateurs
            $grid.Add_SelectionChanged({
                foreach ($row in $grid.SelectedRows) {
                    if ($row.Cells["Champ"].Value -eq "*") {
                        $row.Selected = $false
                    }
                }
            })

            # EmpĂȘcher l'Ă©dition directe des sĂ©parateurs (au cas oĂč)
            $grid.Add_CellBeginEdit({
                param($sender,$e)
                $r = $grid.Rows[$e.RowIndex]
                if ($r.Cells["Champ"].Value -eq "*") {
                    $e.Cancel = $true
                }
            })

            $detailForm.Controls.Add($grid)

            # Bouton Sauvegarder (multi-fichiers)
            $btnSave = New-Object System.Windows.Forms.Button
            $btnSave.Text = "đŸ’Ÿ Sauvegarder"
            $btnSave.Location = New-Object System.Drawing.Point(600,470)
            $btnSave.Size = New-Object System.Drawing.Size(120,35)
            $btnSave.BackColor = [System.Drawing.Color]::FromArgb(70,120,90)
            $btnSave.ForeColor = [System.Drawing.Color]::White
            $btnSave.FlatStyle = "Flat"
            $btnSave.Font = New-Object System.Drawing.Font("Segoe UI", 11, [System.Drawing.FontStyle]::Bold)

            $btnSave.Add_Click({
                # Reconstituer les groupes par fichier à partir des séparateurs
                $groups = @()
                $current = @{ FileName = $null; Rows = @() }

                foreach ($row in $table.Rows) {
                    if ($row["Champ"] -eq "*") {
                        # Si on avait un groupe en cours, on le pousse
                        if ($current.FileName) { $groups += $current }
                        # Nouveau groupe
                        $fileName = ($row["Valeur"] -replace "📂 Fichier : ","").Trim()
                        $current = @{ FileName = $fileName; Rows = @() }
                    } else {
                        if ($current.FileName) { $current.Rows += $row }
                    }
                }
                # Pousser le dernier groupe
                if ($current.FileName) { $groups += $current }

                # Sauvegarde par fichier
                foreach ($g in $groups) {
                    $fullPath = Join-Path $rootPath $g.FileName
                    if (-not (Test-Path $fullPath)) { continue }

                    $data = Import-Csv $fullPath
                    foreach ($row in $g.Rows) {
                        $champ  = $row["Champ"]
                        $valeur = $row["Valeur"]

                        foreach ($line in $data) {
                            if ($line.Contrat -eq $policeId -or $line.Police -eq $policeId) {
                                $line.$champ = $valeur
                            }
                        }
                    }
                    $data | Export-Csv $fullPath -NoTypeInformation -Encoding UTF8
                }

                [System.Windows.Forms.MessageBox]::Show(
                    "✅ Modifications sauvegardĂ©es dans tous les fichiers CSV.",
                    "SuccĂšs",
                    [System.Windows.Forms.MessageBoxButtons]::OK,
                    [System.Windows.Forms.MessageBoxIcon]::Information
                )
            })
            $detailForm.Controls.Add($btnSave)

            # Bouton Fermer
            $btnClose = New-Object System.Windows.Forms.Button
            $btnClose.Text = "✖ Fermer"
            $btnClose.Location = New-Object System.Drawing.Point(740,470)
            $btnClose.Size = New-Object System.Drawing.Size(120,35)
            $btnClose.BackColor = [System.Drawing.Color]::FromArgb(90,90,120)
            $btnClose.ForeColor = [System.Drawing.Color]::White
            $btnClose.FlatStyle = "Flat"
            $btnClose.Font = New-Object System.Drawing.Font("Segoe UI", 11, [System.Drawing.FontStyle]::Bold)
            $btnClose.Add_Click({ $detailForm.Close() })
            $detailForm.Controls.Add($btnClose)

            $detailForm.ShowDialog($form)
        }
        else {
            Show-CustomError $policeId
        }
    }
})

Créé il y a 2 semaines.

Rechercher un Pastebin

Aucun paste trouvé.