CSV-Datei in PrimalForms einbinden
Hallo da draussen,
nach dem mein Chef meine ersten Versuche mit Powershell begutachtet hat sind ihm gleich ein paar neue Aufgaben für mich eingefallen.
Wir werden uns also mit sehr hoher Warscheinlichkeit die nähsten Wochen öffters lesen.
Aber erst ein mal würde ich gern wissen wie ich mir eine CSV-Datei grafisch anzeigen lassen kann. In PrimalForms giebt es die Forms DataGrid und DataGridView. Sind das die richtigen Componenten für die Darstellung oder liege ich hier falsch. Wenn ja wird ich gern wissen wie ich die Daten an die Forms binden kann. Wenn nein was muss ich tun.
nach dem mein Chef meine ersten Versuche mit Powershell begutachtet hat sind ihm gleich ein paar neue Aufgaben für mich eingefallen.
Wir werden uns also mit sehr hoher Warscheinlichkeit die nähsten Wochen öffters lesen.
Aber erst ein mal würde ich gern wissen wie ich mir eine CSV-Datei grafisch anzeigen lassen kann. In PrimalForms giebt es die Forms DataGrid und DataGridView. Sind das die richtigen Componenten für die Darstellung oder liege ich hier falsch. Wenn ja wird ich gern wissen wie ich die Daten an die Forms binden kann. Wenn nein was muss ich tun.
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 207760
Url: https://administrator.de/forum/csv-datei-in-primalforms-einbinden-207760.html
Ausgedruckt am: 10.04.2025 um 23:04 Uhr
17 Kommentare
Neuester Kommentar
Hi Gundelputz,
das DataGrid-View Steuerelement ist Overkill für deinen Zweck, so einfach wie du dir das füllen des Steuerelements vorstellst geht das nicht. Dazu brauchst du noch eine DataTable und eine BindingSource (siehe hier)
Für deinen Zweck reicht ein ListView Objekt.
In Zeile 23 noch deine CSV-Datei eintragen.
Beispiel-Code zum anzeigen einer CSV:
Grüße Uwe
das DataGrid-View Steuerelement ist Overkill für deinen Zweck, so einfach wie du dir das füllen des Steuerelements vorstellst geht das nicht. Dazu brauchst du noch eine DataTable und eine BindingSource (siehe hier)
Für deinen Zweck reicht ein ListView Objekt.
In Zeile 23 noch deine CSV-Datei eintragen.
Beispiel-Code zum anzeigen einer CSV:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#Helper Functions
Function Get-Matches($Pattern,$groupNumber = 0) {begin { $regex = New-Object Regex($pattern) };process { foreach ($match in ($regex.Matches($_))) { ([Object[]]$match.Groups)[$groupNumber].Value }}}
#Generated Form Function
function GenerateForm {
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
#endregion
#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$txtDelim = New-Object System.Windows.Forms.TextBox
$label2 = New-Object System.Windows.Forms.Label
$btnChoose = New-Object System.Windows.Forms.Button
$label1 = New-Object System.Windows.Forms.Label
$txtCSV = New-Object System.Windows.Forms.TextBox
$lv1 = New-Object System.Windows.Forms.ListView
$btnLoadData = New-Object System.Windows.Forms.Button
$openFileDialog1 = New-Object System.Windows.Forms.OpenFileDialog
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
$handler_btnLoadData_Click=
{
if (Test-Path $txtCSV.text){
$lv1.Clear()
$csv = Get-Content $txtCSV.text
$csvSplitChar = $txtDelim.text
#create columns
$firstLine = $csv.split($csvSplitChar)
for ($i = 0; $i -lt $firstLine.length; $i++){
$lv1.Columns.Add($firstLine[$i].Trim('"'))
}
#import data
for ($i = 1; $i -lt $csv.length; $i++){
$lineArray = $csv[$i].split($csvSplitChar)
$listViewItem = New-Object System.Windows.Forms.ListViewItem($lineArray.Trim('"'))
for ($y = 1; $y -lt $lineArray.Length; $y++){
$listViewItem.Subitems.Add($lineArray[$y].Trim('"'))
}
$lv1.Items.Add($listViewItem)
}
$lv1.AutoResizeColumns(2)
}else{
[Windows.Forms.MessageBox]::Show("Datei existiert nicht")
}
}
$btnChoose_OnClick=
{
$openFileDialog1.Filter = "CSV-Dateien(*.csv)|*.csv"
$openFileDialog1.MultiSelect = $False
if ($openFileDialog1.ShowDialog() -eq 1){
$txtCSV.text = $openFileDialog1.FileName
}
#Erkenne Trennzeichen der CSV-Datei
$csvString = (Get-Content $txtCSV.text)
$delim = $csvString | Get-Matches "^[^;,]*(.)" 1
$txtDelim.text = $delim
}
$txtCSV_TextChanged =
{
if ($txtCSV.text -le ""){
$btnLoadData.Enabled = $False
}else{
$btnLoadData.Enabled = $True
}
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 308
$System_Drawing_Size.Width = 441
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 320
$System_Drawing_Size.Width = 320
$form1.MinimumSize = $System_Drawing_Size
$form1.Name = "form1"
$form1.Text = "CSV-Daten anzeigen"
$txtDelim.DataBindings.DefaultDataSourceUpdateMode = 0
$txtDelim.Anchor = 6
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 80
$System_Drawing_Point.Y = 278
$txtDelim.Location = $System_Drawing_Point
$txtDelim.MaxLength = 1
$txtDelim.Name = "txtDelim"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 27
$txtDelim.Size = $System_Drawing_Size
$txtDelim.TabIndex = 7
$txtDelim.Text = ","
$form1.Controls.Add($txtDelim)
$label2.DataBindings.DefaultDataSourceUpdateMode = 0
$label2.Anchor = 6
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 281
$label2.Location = $System_Drawing_Point
$label2.Name = "label2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 78
$label2.Size = $System_Drawing_Size
$label2.TabIndex = 6
$label2.Text = "Trennz."
$form1.Controls.Add($label2)
$btnChoose.Anchor = 10
$btnChoose.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 298
$System_Drawing_Point.Y = 253
$btnChoose.Location = $System_Drawing_Point
$btnChoose.Name = "btnChoose"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 31
$btnChoose.Size = $System_Drawing_Size
$btnChoose.TabIndex = 5
$btnChoose.Text = "..."
$btnChoose.UseVisualStyleBackColor = $True
$btnChoose.add_Click($btnChoose_OnClick)
$form1.Controls.Add($btnChoose)
$label1.Anchor = 6
$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 258
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 62
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 4
$label1.Text = "CSV-Datei:"
$form1.Controls.Add($label1)
$txtCSV.Anchor = 14
$txtCSV.DataBindings.DefaultDataSourceUpdateMode = 0
$txtCSV.Enabled = $False
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 80
$System_Drawing_Point.Y = 255
$txtCSV.Location = $System_Drawing_Point
$txtCSV.Name = "txtCSV"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 210
$txtCSV.Size = $System_Drawing_Size
$txtCSV.TabIndex = 3
$txtCSV.add_TextChanged($txtCSV_TextChanged)
$form1.Controls.Add($txtCSV)
$lv1.Anchor = 15
$lv1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$lv1.Location = $System_Drawing_Point
$lv1.Name = "lv1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 235
$System_Drawing_Size.Width = 417
$lv1.Size = $System_Drawing_Size
$lv1.TabIndex = 2
$lv1.UseCompatibleStateImageBehavior = $False
$lv1.View = 1
$form1.Controls.Add($lv1)
$btnLoadData.Anchor = 10
$btnLoadData.DataBindings.DefaultDataSourceUpdateMode = 0
$btnLoadData.Enabled = $False
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 335
$System_Drawing_Point.Y = 253
$btnLoadData.Location = $System_Drawing_Point
$btnLoadData.Name = "btnLoadData"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 94
$btnLoadData.Size = $System_Drawing_Size
$btnLoadData.TabIndex = 1
$btnLoadData.Text = "Lade Daten"
$btnLoadData.UseVisualStyleBackColor = $True
$btnLoadData.add_Click($handler_btnLoadData_Click)
$form1.Controls.Add($btnLoadData)
$openFileDialog1.FileName = "fd1"
$openFileDialog1.ShowHelp = $True
#endregion Generated Form Code
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
} #End Function
#Call the Function
GenerateForm
Grüße Uwe
ist hier die vb-version zutreffend?
.NET-Objekte sind generisch d.h. wenn du wissen willst welche Funktionen und Eigenschaften ein Objekt hat siehe in der Klassenbibliothek des .NET-Frameworks nach. Die Codekonventionen der Powershell musst du dabei natürlich einhalten, einfaches Kopieren und Einfügen wird hier nicht klappen...
Die Seite sollte deine Fragen beantworten: http://msdn.microsoft.com/de-de/library/system.windows.forms.listview.a ...
Den Index des aktuell markierten Elemente findest du mit .SelectedIndices oder mit .SelectedItems die jeweilig markierten Einträge.
Ich würde alle Bearbeitungen im ListView-Element vornehmen und
dann zum Schluss alle Elemente des ListView-Elements per Schleife durchlaufen und die vorhandene CSV ersetzen. Die Spaltennamen nicht vergessen...
Den Index des aktuell markierten Elemente findest du mit .SelectedIndices oder mit .SelectedItems die jeweilig markierten Einträge.
Ich würde alle Bearbeitungen im ListView-Element vornehmen und
dann zum Schluss alle Elemente des ListView-Elements per Schleife durchlaufen und die vorhandene CSV ersetzen. Die Spaltennamen nicht vergessen...
den Text der Spalten des markierten Elements erhältst du so:
usw.
da eine Markierung in einer ListView mehrere Einträge umfassen kann wenn die MultiSelect Eigenschaft des Steuerelements auf True steht, befinden sich diese im Array
Mit den
Wenn du Einträge bearbeiten willst ist das DataGridView Element aber besser geeignet, ich dachte du wolltest die CSV nur anzeigen.
Ich glaube es wäre vielleicht besser, wenn du dich mal mit einer richtigen Programmiersprache wie VB.Net oder C# in einer Entwicklungsumgebung wie einer VisualStudio Express Edition auseinandersetzt, Powershell ist ja eigentlich nur eine Scriptsprache. Dort hast du auch visuelles Feedback der Steuerelemente mit Code-Completion etc. Das was du dort entwickelst lässt sich dann mit ein wenig Anpassung in die Powershell übertragen... das nur so als Tipp nebenbei
1
2
3
2
3
$spalte1 = $lv_user.SelectedItems.Text
$spalte2 = $lv_user.SelectedItems.Subitems[1].Text
$spalte3 = $lv_user.SelectedItems.Subitems[2].Text
da eine Markierung in einer ListView mehrere Einträge umfassen kann wenn die MultiSelect Eigenschaft des Steuerelements auf True steht, befinden sich diese im Array
.SelectedItems[x]
, d.h. erste markierte Zeile .SelectedItems
, zweite .SelectedItems[1]
, usw.Mit den
ListIndices
verhällt es sich genauso. D.h. die Indexnummern der markierten Einträge werden als Array zurückgegeben. Bei nur einer einfachen Markierung erhältst du den Index mit .ListIndices
.Wenn du Einträge bearbeiten willst ist das DataGridView Element aber besser geeignet, ich dachte du wolltest die CSV nur anzeigen.
Ich glaube es wäre vielleicht besser, wenn du dich mal mit einer richtigen Programmiersprache wie VB.Net oder C# in einer Entwicklungsumgebung wie einer VisualStudio Express Edition auseinandersetzt, Powershell ist ja eigentlich nur eine Scriptsprache. Dort hast du auch visuelles Feedback der Steuerelemente mit Code-Completion etc. Das was du dort entwickelst lässt sich dann mit ein wenig Anpassung in die Powershell übertragen... das nur so als Tipp nebenbei
Um das ganze zu einem Abschluss zu bringen:
Das ganze mit einem DataGridView Element und vollständiger Bearbeitungsfunktionalität und automatischer Trennzeichenerkennung:
Viel Erfolg weiterhin...
Uwe
Das ganze mit einem DataGridView Element und vollständiger Bearbeitungsfunktionalität und automatischer Trennzeichenerkennung:
Viel Erfolg weiterhin...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#Helper Functions
#Hilfsfunktion zum Regex-Gruppen-Matching
Function Get-Matches($Pattern,$groupNumber = 0) {begin { $regex = New-Object Regex($pattern) };process { foreach ($match in ($regex.Matches($_))) { ([Object[]]$match.Groups)[$groupNumber].Value }}}
#Generated Form Function
function GenerateForm {
#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
#endregion
#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$btnSave = New-Object System.Windows.Forms.Button
$dg1 = New-Object System.Windows.Forms.DataGridView
$txtDelim = New-Object System.Windows.Forms.TextBox
$label2 = New-Object System.Windows.Forms.Label
$btnChoose = New-Object System.Windows.Forms.Button
$label1 = New-Object System.Windows.Forms.Label
$txtCSV = New-Object System.Windows.Forms.TextBox
$btnLoadData = New-Object System.Windows.Forms.Button
$openFileDialog1 = New-Object System.Windows.Forms.OpenFileDialog
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
$handler_btnLoadData_Click=
{
#prüfen ob gewählte Datei existiert
if (Test-Path $txtCSV.text){
$csvSplitChar = $txtDelim.text
$dg1.Rows.Clear()
#Lese CSV-Datei in ein StreamReader-Objekt ein / $true = erkenne Zeichenkodierung
$csv = New-Object System.IO.StreamReader($txtCSV.text,$True)
#Lese die Überschriften der CSV-Datei in ein Array ein
$firstLine = ($csv.Readline()).split($csvSplitChar)
#Füge die Überschriften im DataGridView-Element hinzu
for ($i = 0; $i -lt $firstLine.length; $i++){
#entferne Dabei eventuell vorhandene Anführungszeichen
$dg1.Columns.Add($firstLine[$i].Trim('"'),$firstLine[$i].Trim('"'))
}
#Durchlaufe die Datenzeilen der CSV-Datei bis zum Ende
while($csv.Peek() -ne -1){
$dataline = $csv.ReadLine()
#Splite die Zeile wieder in ein Array
$dataline = $dataline.split($csvSplitChar) | %{$_.Trim('"')}
# und übergebe das Array an die Funktion zum erstellen einer Zeile des DataGridView-Elementes
$dg1.Rows.Add($dataline)
}
#schließe das StreamReader-Objekt
$csv.Close()
}else{
[Windows.Forms.MessageBox]::Show("Datei existiert nicht")
}
}
$btnChoose_OnClick=
{
#setze Filter für den FileDialog
$openFileDialog1.Filter = "CSV-Dateien(*.csv)|*.csv"
#es darf im Dialog nur eine Datei gewählt werden
$openFileDialog1.MultiSelect = $False
# wenn der Dialog mit "OK" geschlossen wurde den Pfad der Datei in die Textbox übergeben
if ($openFileDialog1.ShowDialog() -eq 1){
$txtCSV.text = $openFileDialog1.FileName
}
#Erkenne Trennzeichen der CSV-Datei
#hole nur die erste Zeile der Datei
$csvString = (Get-Content $txtCSV.text)
#finde per RegEx das Trennzeichen
$delim = $csvString | Get-Matches "^[^;,]*(.)" 1
$txtDelim.text = $delim
}
# Schaltet die Buttons je nach Inhalt des Textfeldes an oder aus / wenn kein Pfad vorhanden soll man die Buttons auch nicht betätigen können
$handler_txtCSV_TextChanged =
{
if ($txtCSV.text -le ""){
$btnLoadData.Enabled = $False
$btnSave.Enabled = $False
}else{
$btnLoadData.Enabled = $True
}
}
$handler_dg1_CellValueChanged =
{
#aktiviert den "Speichern"-Button erst wenn etwas in der Tabelle geändert wurde
$btnSave.Enabled = $True
}
$btnSave_OnClick=
{
#StreamWrite-Objekt für das schreiben der Ausgabedatei / "$false" bedeutet dabei Datei neu erstellen und nicht anhängen
$objWriter = New-Object System.IO.StreamWriter($txtCSV.text, $False)
#Erstelle die Spaltennamen für die erste Zeile der CSV
$headerline = ""
#Hole die Spaltennamen aus dem DataGridView-Element
for ($i = 0; $i -lt $dg1.Columns.Count; $i++){
if ($i -eq $dg1.Columns.Count -1){
#Spaltenname ohne Trennzeichen anhängen wenn wir bei der letzten Spalte angekommen sind
$headerline += '"' + $dg1.Columns[$i].Name + '"'
}else{
#ansonsten mit Trennzeichen
$headerline += '"' + $dg1.Columns[$i].Name + '"' + $txtDelim.text
}
}
#Schreibe die Überschriften in die Datei
$objWriter.WriteLine($headerline)
#Schreibe die Datenzeilen des DataGridView in die Datei
For ($j = 0;$j -lt $dg1.Rows.Count -1 ; $j++){
For ($i = 0;$i -lt $dg1.Columns.Count ; $i++){
$cellvalue = $dg1.Item($i, $j).Value
if ($i -eq $dg1.Columns.Count - 1){
$rowLine += '"' + $cellvalue + '"'
}else{
$rowLine += '"' + $cellvalue + '"' + $txtDelim.text
}
}
#Die Zeile in die Datei schreiben
$objWriter.WriteLine($rowLine)
#Variable leeren für die nächste Zeile
$rowLine = ""
}
#Streamwriter schließen
$objWriter.Close()
[Windows.Forms.MessageBox]::Show("Datei gespeichert!")
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 292
$System_Drawing_Size.Width = 467
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 320
$System_Drawing_Size.Width = 320
$form1.MinimumSize = $System_Drawing_Size
$form1.Name = "form1"
$form1.Text = "CSV Bearbeiten"
$btnSave.DataBindings.DefaultDataSourceUpdateMode = 0
$btnSave.Anchor = 10
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 361
$System_Drawing_Point.Y = 266
$btnSave.Location = $System_Drawing_Point
$btnSave.Name = "btnSave"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 94
$btnSave.Size = $System_Drawing_Size
$btnSave.TabIndex = 9
$btnSave.Text = "Daten speichern"
$btnSave.UseVisualStyleBackColor = $True
$btnSave.add_Click($btnSave_OnClick)
$btnSave.Enabled = $False
$form1.Controls.Add($btnSave)
$dg1.Anchor = 15
$dg1.AllowUserToOrderColumns = $True
$dg1.MultiSelect = $True
$dg1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$dg1.Location = $System_Drawing_Point
$dg1.Name = "dg1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 219
$System_Drawing_Size.Width = 443
$dg1.Size = $System_Drawing_Size
$dg1.TabIndex = 8
$dg1.add_CellValueChanged($handler_dg1_CellValueChanged)
$form1.Controls.Add($dg1)
$txtDelim.Anchor = 6
$txtDelim.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 80
$System_Drawing_Point.Y = 262
$txtDelim.Location = $System_Drawing_Point
$txtDelim.MaxLength = 1
$txtDelim.Name = "txtDelim"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 27
$txtDelim.Size = $System_Drawing_Size
$txtDelim.TabIndex = 7
$txtDelim.Text = ","
$form1.Controls.Add($txtDelim)
$label2.Anchor = 6
$label2.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 265
$label2.Location = $System_Drawing_Point
$label2.Name = "label2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 78
$label2.Size = $System_Drawing_Size
$label2.TabIndex = 6
$label2.Text = "Trennz."
$form1.Controls.Add($label2)
$btnChoose.Anchor = 10
$btnChoose.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 324
$System_Drawing_Point.Y = 237
$btnChoose.Location = $System_Drawing_Point
$btnChoose.Name = "btnChoose"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 31
$btnChoose.Size = $System_Drawing_Size
$btnChoose.TabIndex = 5
$btnChoose.Text = "..."
$btnChoose.UseVisualStyleBackColor = $True
$btnChoose.add_Click($btnChoose_OnClick)
$form1.Controls.Add($btnChoose)
$label1.Anchor = 6
$label1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 242
$label1.Location = $System_Drawing_Point
$label1.Name = "label1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 62
$label1.Size = $System_Drawing_Size
$label1.TabIndex = 4
$label1.Text = "CSV-Datei:"
$form1.Controls.Add($label1)
$txtCSV.Anchor = 14
$txtCSV.DataBindings.DefaultDataSourceUpdateMode = 0
$txtCSV.Enabled = $False
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 80
$System_Drawing_Point.Y = 239
$txtCSV.Location = $System_Drawing_Point
$txtCSV.Name = "txtCSV"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 236
$txtCSV.Size = $System_Drawing_Size
$txtCSV.TabIndex = 3
$txtCSV.add_TextChanged($handler_txtCSV_TextChanged)
$form1.Controls.Add($txtCSV)
$btnLoadData.Anchor = 10
$btnLoadData.DataBindings.DefaultDataSourceUpdateMode = 0
$btnLoadData.Enabled = $False
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 361
$System_Drawing_Point.Y = 237
$btnLoadData.Location = $System_Drawing_Point
$btnLoadData.Name = "btnLoadData"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 23
$System_Drawing_Size.Width = 94
$btnLoadData.Size = $System_Drawing_Size
$btnLoadData.TabIndex = 1
$btnLoadData.Text = "Lade Daten"
$btnLoadData.UseVisualStyleBackColor = $True
$btnLoadData.add_Click($handler_btnLoadData_Click)
$form1.Controls.Add($btnLoadData)
$openFileDialog1.FileName = "fd1"
$openFileDialog1.ShowHelp = $True
#endregion Generated Form Code
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
} #End Function
#Call the Function
GenerateForm