Question remplir une DATAGRID avec les fichier d'un réperto

Plus d'informations
il y a 15 ans 8 mois #7312 par zabimaru27
bonjour, donc voilà mon problème.
J'ai créer un windows form, avec un bouton et un datagrid.

Je voudrais que au moment de l'exécution du script, le data grid soit remplit avec les nom de fichier contenu dans un répertoire particulier.

Voici l'armature du code
[code:1]
#Generated Form Function
function GenerateForm {

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname(\"System.Windows.Forms\"«») | Out-Null
[reflection.assembly]::loadwithpartialname(\"System.Drawing\"«») | Out-Null
#endregion

#region Generated Form Objects
$APPV_form1 = New-Object System.Windows.Forms.Form
$liste_package_dataGrid = New-Object System.Windows.Forms.DataGrid
$bouton_lancerCT = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#
#Generated Event Script Blocks
#
#Provide Custom Code for events specified in PrimalForms.
$bouton_lancerCT_OnClick=
{
#TODO: Place custom script here

}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$APPV_form1.WindowState = $InitialFormWindowState
}

#
#region Generated Form Code
$APPV_form1.Text = \"APPV CT\"
$APPV_form1.Name = \"APPV_form1\"
$APPV_form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 777
$System_Drawing_Size.Height = 568
$APPV_form1.ClientSize = $System_Drawing_Size

$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 656
$System_Drawing_Size.Height = 363
$liste_package_dataGrid.Size = $System_Drawing_Size
$liste_package_dataGrid.DataBindings.DefaultDataSourceUpdateMode = 0
$liste_package_dataGrid.HeaderForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
$liste_package_dataGrid.Name = \"liste_package_dataGrid\"
$liste_package_dataGrid.DataMember = \"\"
$liste_package_dataGrid.TabIndex = 1
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 70
$System_Drawing_Point.Y = 40
$liste_package_dataGrid.Location = $System_Drawing_Point

$APPV_form1.Controls.Add($liste_package_dataGrid)

$bouton_lancerCT.TabIndex = 0
$bouton_lancerCT.Name = \"bouton_lancerCT\"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 139
$System_Drawing_Size.Height = 71
$bouton_lancerCT.Size = $System_Drawing_Size
$bouton_lancerCT.UseVisualStyleBackColor = $True

$bouton_lancerCT.Text = \"Lancer CT\"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 70
$System_Drawing_Point.Y = 418
$bouton_lancerCT.Location = $System_Drawing_Point
$bouton_lancerCT.DataBindings.DefaultDataSourceUpdateMode = 0
$bouton_lancerCT.add_Click($bouton_lancerCT_OnClick)

$APPV_form1.Controls.Add($bouton_lancerCT)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $APPV_form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$APPV_form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$APPV_form1.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm

[/code:1]

Merci d'avance

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 15 ans 8 mois #7325 par zabimaru27
bonjour a tous.

bon j'ai réussi en partie
J'utilise le code suivant

[code:1]
$list = get-childitem c:\
$tab_file = new-object system.Collections.ArrayList($null)
$tab_file.addRange($list)
$liste_pacckage_datagrid.datasource = $tab_file
[/code:1]

Cela fonctionne en partie

Ca m'importe bien les fichiers dans la data grid, le rpoblème c'est que j'ai beaucoup de colonnes qui ne servent à rien. Je ne sais pas comment filtrer.

de plus il doit y avoir un problème car lorsque je déplace la barre de défilement sur la gauche pour voir les infos des autre colonne, la form plante et j'ai deux grosse croix rouge à la place de la datagrid.

Plz aidez moi

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 15 ans 8 mois #7327 par Laurent Dardenne
Salut,
zabimaru27 écrit:

Ca m'importe bien les fichiers dans la data grid

Ton erreur est là, le résultat du cmdlet Get-ChildItem ne renvoi pas QUE des fichiers, mais aussi des répertoires.
zabimaru27 écrit:

Je ne sais pas comment filtrer.

C'est l'objet que tu inséres dans l'arraylist que tu dois filtrer, enfin c'est une des possibilités.
zabimaru27 écrit:

la form plante

Pb dû à une liste d'objet hétérogène ( fichier et répertoires). Comme les deux classes ne proposent pas les même propriétés certaines ne pouvant être affichées, le composant plante lors de l'affichage :
[code:1]
Dir C:\|Get-Member -Member Property*
[/code:1]
Il ne faut afficher que des fichiers ou que des répertoires ou encore que des objet personnalisés.
[code:1]
#Préallocation de la liste à 50 éléments
$tab_file = new-object system.Collections.ArrayList 50

Get-ChildItem C:\|
#Filtre les répertoires
Where {$_.PSisContainer -eq $false}|
#Ajoute toutes les propriétés de l'objet de type fichier
Foreach {[void]$tab_file.add($_) }
#Ajoute certaines propriétés de l'objet de type fichier
#Foreach {[void]$tab_file.add(($_|Select Name,Lenght,isReadOnly)) }

$liste_package_datagrid.datasource = $tab_file
[/code:1]
Pour afficher les répertoires il faut soit ajouter une seconde grille soit construire une liste d'objet personnalisé homogéne:
[code:1]
$MesObjets=Dir C:\|
Select Name,Extension,Attributes,@{Name='Type';Expression={if ($_.PsIsContainer) {\"Répertoire\"} else {\"Fichier\"}}
#
Get-ChildItem c:\|
Foreach {
[void]$tab_file.add(($_|
Select Name,Extension,Attributes,@{Name='Type';
Expression={if ($_.PsIsContainer)
{\"Répertoire\"}
else
{\"Fichier\"}
}
}
))
}#foreach


[/code:1]

Tutoriels PowerShell

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.041 secondes
Propulsé par Kunena