回答編集履歴

2

見直しキャンペーン中

2023/07/20 14:32

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -1,637 +1,319 @@
1
1
  DataGridは、行をひとつの塊として追加していく作りです(有償のものですと行列を簡単に入れ替えられるものがあるようです)
2
2
 
3
-
4
-
5
3
  ですので、
6
-
7
4
  ||AAA|BBB|CCC|DDD|
8
-
9
5
  |:---|---:|---:|---:|---:|
10
-
11
6
  |001|1|11|1.1|11.1|
12
-
13
7
  |002|2|22|2.2|22.2|
14
-
15
8
  |003|3|33|3.3|33.3|
16
9
 
17
-
18
-
19
10
  のような形でよければ簡単です(DataGrid_CSV1・NewMethod1 無駄に凝ってしまったので、あんまり簡単に見えませんが^^;
20
11
 
21
-
22
-
23
12
  ---
24
13
 
25
-
26
-
27
14
  方向にこだわる場合は、`List<List<string>>`にして列を追加していく感じがいいんじゃないでしょうか(DataGrid_CSV2・NewMethod2)
28
15
 
29
16
 
30
-
31
-
32
-
33
- ```xaml
17
+ ```xml
34
-
35
18
  <Window
36
-
37
19
  x:Class="Questions241581.MainWindow"
38
-
39
20
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
40
-
41
21
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
42
-
43
22
  Width="800"
44
-
45
23
  Height="450">
46
-
47
24
  <Grid>
48
-
49
25
  <Grid.RowDefinitions>
50
-
51
26
  <RowDefinition />
52
-
53
27
  <RowDefinition />
54
-
55
28
  </Grid.RowDefinitions>
56
-
57
29
  <DataGrid
58
-
59
30
  x:Name="DataGrid_CSV1"
60
-
61
31
  AutoGenerateColumns="False"
62
-
63
32
  HeadersVisibility="All">
64
-
65
33
  <!-- 行ヘッダーにしてみた -->
66
-
67
34
  <DataGrid.RowHeaderStyle>
68
-
69
35
  <Style TargetType="{x:Type DataGridRowHeader}">
70
-
71
36
  <Setter Property="Content" Value="{Binding [0]}" />
72
-
73
37
  </Style>
74
-
75
38
  </DataGrid.RowHeaderStyle>
76
-
77
39
  <DataGrid.Columns>
78
-
79
40
  <!--<DataGridTextColumn Binding="{Binding [0]}" Header="Param" />-->
80
-
81
41
  <DataGridTextColumn Binding="{Binding [1]}" />
82
-
83
42
  <DataGridTextColumn Binding="{Binding [2]}" />
84
-
85
43
  <DataGridTextColumn Binding="{Binding [3]}" />
86
-
87
44
  <DataGridTextColumn Binding="{Binding [4]}" />
88
-
89
45
  <!--<DataGridTextColumn Binding="{Binding [5]}" />-->
90
-
91
46
  </DataGrid.Columns>
92
-
93
47
  </DataGrid>
94
48
 
95
-
96
-
97
49
  <DataGrid
98
-
99
50
  x:Name="DataGrid_CSV2"
100
-
101
51
  Grid.Row="1"
102
-
103
52
  AutoGenerateColumns="False"
104
-
105
53
  HeadersVisibility="All">
106
-
107
54
  <!-- 行ヘッダーにしてみた -->
108
-
109
55
  <DataGrid.RowHeaderStyle>
110
-
111
56
  <Style TargetType="{x:Type DataGridRowHeader}">
112
-
113
57
  <Setter Property="Content" Value="{Binding [0]}" />
114
-
115
58
  </Style>
116
-
117
59
  </DataGrid.RowHeaderStyle>
118
-
119
60
  <DataGrid.Columns>
120
-
121
61
  <!--<DataGridTextColumn Binding="{Binding [0]}" Header="Param" />-->
122
-
123
62
  <DataGridTextColumn Binding="{Binding [1]}" Header="Val_001" />
124
-
125
63
  <DataGridTextColumn Binding="{Binding [2]}" Header="Val_002" />
126
-
127
64
  <DataGridTextColumn Binding="{Binding [3]}" Header="Val_003" />
128
-
129
65
  <DataGridTextColumn Binding="{Binding [4]}" Header="Val_004" />
130
-
131
66
  <DataGridTextColumn Binding="{Binding [5]}" Header="Val_005" />
132
-
133
67
  </DataGrid.Columns>
134
-
135
68
  </DataGrid>
136
-
137
69
  </Grid>
138
-
139
70
  </Window>
140
-
141
71
  ```
142
-
143
- ```C#
72
+ ```cs
144
-
145
73
  using System;
146
-
147
74
  using System.Collections.Generic;
148
-
149
75
  using System.Data;
150
-
151
76
  using System.IO;
152
-
153
77
  using System.Linq;
154
-
155
78
  using System.Text;
156
-
157
79
  using System.Windows;
158
80
 
159
-
160
-
161
81
  namespace Questions241581
162
-
163
82
  {
164
-
165
83
  public partial class MainWindow : Window
166
-
167
84
  {
168
-
169
85
  public MainWindow()
170
-
171
- {
86
+ {
172
-
173
87
  InitializeComponent();
174
88
 
175
-
176
-
177
89
  NewMethod1();
178
-
179
90
  NewMethod2();
180
-
181
- }
91
+ }
182
-
183
-
184
92
 
185
93
  private void NewMethod1()
186
-
187
- {
94
+ {
188
-
189
95
  List<string[]> all_data = new List<string[]>();
190
96
 
191
-
192
-
193
97
  for(int i = 1; i <= 5; i++)
194
-
195
- {
98
+ {
196
-
197
99
  string path_read = @"Param_" + i.ToString("000") + ".csv";
198
100
 
199
-
200
-
201
101
  List<string[]> list_file = new List<string[]>();
202
102
 
203
-
204
-
205
103
  // 行ヘッダーになる部分("Val001")を先に追加しておく *1
206
-
207
104
  list_file.Add(new string[] { "", @"Val" + i.ToString("000") });
208
-
209
105
  FileRead(path_read, list_file);
210
106
 
211
-
212
-
213
107
  // 最初のファイルの時は"AAA"部分を列ヘッダーに設定する
214
-
215
108
  if(i == 1)
216
-
217
- {
109
+ {
218
-
219
110
  // list_fileから0番目の要素("AAA"等)を集めて配列にする
220
-
221
111
  // *1で""が入ってしまっているのでいっこ飛ばす(Skip(1))
222
-
223
112
  string[] names = list_file.Skip(1).Select(x => x[0]).ToArray();
224
-
225
113
  for(int j = 0; j < names.Length; j++)
226
-
227
- {
114
+ {
228
-
229
115
  // 列ヘッダーはcsvを読むまで決まらないのでここで設定する
230
-
231
116
  DataGrid_CSV1.Columns[j].Header = names[j];
232
-
233
- }
117
+ }
234
-
235
- }
118
+ }
236
-
237
-
238
119
 
239
120
  // list_fileから1番目の要素("1"等)を集めて配列にする
240
-
241
121
  // *1のおかげで先頭は"Val001"等になっている
242
-
243
122
  string[] values = list_file.Select(x => x[1]).ToArray();
244
-
245
123
  all_data.Add(values);
246
-
247
- }
124
+ }
248
-
249
-
250
125
 
251
126
  DataGrid_CSV1.ItemsSource = all_data;
252
-
253
- }
127
+ }
254
-
255
-
256
128
 
257
129
  private void NewMethod2()
258
-
259
- {
130
+ {
260
-
261
131
  // List<string>のList
262
-
263
132
  // 列を追加していきたいので配列ではなくリストに
264
-
265
133
  // 列数がわかっているので配列でもいいのだが、Listのほうがわかりやすそう
266
-
267
134
  List<List<string>> all_data = new List<List<string>>();
268
135
 
269
-
270
-
271
136
  for(int i = 1; i <= 5; i++)
272
-
273
- {
137
+ {
274
-
275
138
  string path_read = @"Param_" + i.ToString("000") + ".csv";
276
139
 
277
-
278
-
279
140
  List<string[]> list_file = new List<string[]>();
280
-
281
141
  FileRead(path_read, list_file);
282
142
 
283
-
284
-
285
143
  // 最初のファイルの時は内側のList(1行分のデータ)を作成する
286
-
287
144
  if(i == 1)
288
-
289
- {
145
+ {
290
-
291
146
  for(int j = 0; j < list_file.Count; j++)
292
-
293
- {
147
+ {
294
-
295
148
  // 内側のListを作成する
296
-
297
149
  List<string> row_data = new List<string>();
298
150
 
299
-
300
-
301
151
  // j行目の0番目 "AAA"等 を内側のListに追加する
302
-
303
152
  row_data.Add(list_file[j][0]);
304
153
 
305
-
306
-
307
154
  // 外側のListに内側のListを追加する
308
-
309
155
  all_data.Add(row_data);
310
-
311
- }
156
+ }
312
-
313
- }
157
+ }
314
-
315
-
316
158
 
317
159
  // 作成済みの内側のListに各数字を追加していく
318
-
319
160
  for(int j = 0; j < list_file.Count; j++)
320
-
321
- {
161
+ {
322
-
323
162
  // j行目の1番目 "1"等 を内側のListに追加する
324
-
325
163
  all_data[j].Add(list_file[j][1]);
326
-
327
- }
164
+ }
328
-
329
- }
165
+ }
330
-
331
-
332
166
 
333
167
  DataGrid_CSV2.ItemsSource = all_data;
334
-
335
- }
168
+ }
336
-
337
-
338
169
 
339
170
  public void FileRead(string path_read, List<string[]> list_file)
340
-
341
- {
171
+ {
342
-
343
172
  try
344
-
345
- {
173
+ {
346
-
347
174
  using(var sr = new StreamReader(path_read, Encoding.GetEncoding("shift_jis")))
348
-
349
- {
175
+ {
350
-
351
176
  while(!sr.EndOfStream)
352
-
353
- {
177
+ {
354
-
355
178
  string line = sr.ReadLine();
356
-
357
179
  string[] csv_array = line.Split(',');
358
-
359
180
  list_file.Add(csv_array);
360
-
361
- }
181
+ }
362
-
363
- }
182
+ }
364
-
365
- }
183
+ }
366
-
367
184
  catch(Exception e) { }
368
-
369
- }
185
+ }
370
-
371
186
  }
372
-
373
187
  }
374
-
375
188
  ```
376
189
 
377
-
378
-
379
190
  AAA,BBB,CCC,DDDは各ファイルに同じ順にあるとする。
380
-
381
191
  各ファイルは同じ行数で、各行には一つ以上のカンマがあるとする。
382
192
 
383
193
 
384
-
385
-
386
-
387
194
  ---
388
195
 
389
-
390
-
391
196
  追記
392
-
393
197
  値の編集をする場合`DataTable`を使ったほうがお手軽かもしれません。
394
-
395
- ```xaml
198
+ ```xml
396
-
397
199
  <Window
398
-
399
200
  x:Class="Questions241581.MainWindow"
400
-
401
201
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
402
-
403
202
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
404
-
405
203
  Width="800"
406
-
407
204
  Height="450">
408
-
409
205
  <Grid>
410
-
411
206
  <Grid.RowDefinitions>
412
-
413
207
  <RowDefinition />
414
-
415
208
  <RowDefinition Height="Auto" />
416
-
417
209
  </Grid.RowDefinitions>
418
-
419
210
  <DataGrid x:Name="DataGrid_CSV" />
420
-
421
211
  <Button
422
-
423
212
  Grid.Row="1"
424
-
425
213
  Click="Button_Click"
426
-
427
214
  Content="Save" />
428
-
429
215
  </Grid>
430
-
431
216
  </Window>
432
-
433
217
  ```
434
218
 
435
-
436
-
437
- ```C#
219
+ ```cs
438
-
439
220
  using System;
440
-
441
221
  using System.Collections.Generic;
442
-
443
222
  using System.Data;
444
-
445
223
  using System.IO;
446
-
447
224
  using System.Linq;
448
-
449
225
  using System.Text;
450
-
451
226
  using System.Windows;
452
227
 
453
-
454
-
455
228
  namespace Questions241581
456
-
457
229
  {
458
-
459
230
  public partial class MainWindow : Window
460
-
461
231
  {
462
-
463
232
  private DataTable table;
464
233
 
465
-
466
-
467
234
  public MainWindow()
468
-
469
- {
235
+ {
470
-
471
236
  InitializeComponent();
472
237
 
473
-
474
-
475
238
  table = new DataTable();
476
-
477
239
  table.Columns.Add("Param");
478
-
479
240
  table.Columns.Add("Val__001");
480
-
481
241
  table.Columns.Add("Val__002");
482
-
483
242
  table.Columns.Add("Val__003");
484
-
485
243
  table.Columns.Add("Val__004");
486
-
487
244
  table.Columns.Add("Val__005");
488
245
 
489
-
490
-
491
246
  for(int i = 1; i <= 5; i++)
492
-
493
- {
247
+ {
494
-
495
248
  string path_read = @"Param_" + i.ToString("000") + ".csv";
496
249
 
497
-
498
-
499
250
  List<string[]> list_file = new List<string[]>();
500
-
501
251
  FileRead(path_read, list_file);
502
252
 
503
-
504
-
505
253
  // 最初のファイルの時はDataRow(1行分のデータ)を作成する
506
-
507
254
  if(i == 1)
508
-
509
- {
255
+ {
510
-
511
256
  for(int j = 0; j < list_file.Count; j++)
512
-
513
- {
257
+ {
514
-
515
258
  DataRow row = table.NewRow();
516
259
 
517
-
518
-
519
260
  // j行目の0番目 "AAA"等 をDataRowに追加する
520
-
521
261
  row[0] = list_file[j][0];
522
-
523
262
  table.Rows.Add(row);
524
-
525
- }
263
+ }
526
-
527
- }
264
+ }
528
-
529
-
530
265
 
531
266
  // 作成済みのDataRowに各数字を追加していく
532
-
533
267
  for(int j = 0; j < list_file.Count; j++)
534
-
535
- {
268
+ {
536
-
537
269
  table.Rows[j][i] = list_file[j][1];
538
-
539
- }
270
+ }
540
-
541
- }
271
+ }
542
-
543
-
544
272
 
545
273
  DataGrid_CSV.ItemsSource = table.DefaultView;
546
-
547
- }
274
+ }
548
-
549
275
  private void Button_Click(object sender, RoutedEventArgs e)
550
-
551
- {
276
+ {
552
-
553
277
  for(int i = 1; i <= 5; i++)
554
-
555
- {
278
+ {
556
-
557
279
  string path_write = @"new_Param_" + i.ToString("000") + ".csv";
558
280
 
559
-
560
-
561
281
  // 名前と値のペアのリストを作る
562
-
563
282
  var list_file = table.AsEnumerable()
564
-
565
283
  .Select(x => new object[] { x[0], x[i] })
566
-
567
284
  .ToList();
568
285
 
569
-
570
-
571
286
  FileWrite(path_write, list_file);
572
-
573
- }
287
+ }
574
-
575
- }
288
+ }
576
-
577
-
578
289
 
579
290
  public void FileRead(string path_read, List<string[]> list_file)
580
-
581
- {
291
+ {
582
-
583
292
  try
584
-
585
- {
293
+ {
586
-
587
294
  using(var sr = new StreamReader(path_read, Encoding.GetEncoding("shift_jis")))
588
-
589
- {
295
+ {
590
-
591
296
  while(!sr.EndOfStream)
592
-
593
- {
297
+ {
594
-
595
298
  string line = sr.ReadLine();
596
-
597
299
  string[] csv_array = line.Split(',');
598
-
599
300
  list_file.Add(csv_array);
600
-
601
- }
301
+ }
602
-
603
- }
302
+ }
604
-
605
- }
303
+ }
606
-
607
304
  catch(Exception e) { }
608
-
609
- }
305
+ }
610
-
611
-
612
306
 
613
307
  public void FileWrite(string path_write, List<object[]> list_file)
614
-
615
- {
308
+ {
616
-
617
309
  using(var sw = new StreamWriter(path_write, false, Encoding.GetEncoding("shift_jis")))
618
-
619
- {
310
+ {
620
-
621
311
  foreach(object[] csv_array in list_file)
622
-
623
- {
312
+ {
624
-
625
313
  sw.WriteLine(string.Join(",", csv_array));
626
-
627
- }
314
+ }
628
-
629
- }
315
+ }
630
-
631
- }
316
+ }
632
-
633
317
  }
634
-
635
318
  }
636
-
637
319
  ```

1

DataTable

2020/02/16 04:51

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -379,3 +379,259 @@
379
379
  AAA,BBB,CCC,DDDは各ファイルに同じ順にあるとする。
380
380
 
381
381
  各ファイルは同じ行数で、各行には一つ以上のカンマがあるとする。
382
+
383
+
384
+
385
+
386
+
387
+ ---
388
+
389
+
390
+
391
+ 追記
392
+
393
+ 値の編集をする場合`DataTable`を使ったほうがお手軽かもしれません。
394
+
395
+ ```xaml
396
+
397
+ <Window
398
+
399
+ x:Class="Questions241581.MainWindow"
400
+
401
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
402
+
403
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
404
+
405
+ Width="800"
406
+
407
+ Height="450">
408
+
409
+ <Grid>
410
+
411
+ <Grid.RowDefinitions>
412
+
413
+ <RowDefinition />
414
+
415
+ <RowDefinition Height="Auto" />
416
+
417
+ </Grid.RowDefinitions>
418
+
419
+ <DataGrid x:Name="DataGrid_CSV" />
420
+
421
+ <Button
422
+
423
+ Grid.Row="1"
424
+
425
+ Click="Button_Click"
426
+
427
+ Content="Save" />
428
+
429
+ </Grid>
430
+
431
+ </Window>
432
+
433
+ ```
434
+
435
+
436
+
437
+ ```C#
438
+
439
+ using System;
440
+
441
+ using System.Collections.Generic;
442
+
443
+ using System.Data;
444
+
445
+ using System.IO;
446
+
447
+ using System.Linq;
448
+
449
+ using System.Text;
450
+
451
+ using System.Windows;
452
+
453
+
454
+
455
+ namespace Questions241581
456
+
457
+ {
458
+
459
+ public partial class MainWindow : Window
460
+
461
+ {
462
+
463
+ private DataTable table;
464
+
465
+
466
+
467
+ public MainWindow()
468
+
469
+ {
470
+
471
+ InitializeComponent();
472
+
473
+
474
+
475
+ table = new DataTable();
476
+
477
+ table.Columns.Add("Param");
478
+
479
+ table.Columns.Add("Val__001");
480
+
481
+ table.Columns.Add("Val__002");
482
+
483
+ table.Columns.Add("Val__003");
484
+
485
+ table.Columns.Add("Val__004");
486
+
487
+ table.Columns.Add("Val__005");
488
+
489
+
490
+
491
+ for(int i = 1; i <= 5; i++)
492
+
493
+ {
494
+
495
+ string path_read = @"Param_" + i.ToString("000") + ".csv";
496
+
497
+
498
+
499
+ List<string[]> list_file = new List<string[]>();
500
+
501
+ FileRead(path_read, list_file);
502
+
503
+
504
+
505
+ // 最初のファイルの時はDataRow(1行分のデータ)を作成する
506
+
507
+ if(i == 1)
508
+
509
+ {
510
+
511
+ for(int j = 0; j < list_file.Count; j++)
512
+
513
+ {
514
+
515
+ DataRow row = table.NewRow();
516
+
517
+
518
+
519
+ // j行目の0番目 "AAA"等 をDataRowに追加する
520
+
521
+ row[0] = list_file[j][0];
522
+
523
+ table.Rows.Add(row);
524
+
525
+ }
526
+
527
+ }
528
+
529
+
530
+
531
+ // 作成済みのDataRowに各数字を追加していく
532
+
533
+ for(int j = 0; j < list_file.Count; j++)
534
+
535
+ {
536
+
537
+ table.Rows[j][i] = list_file[j][1];
538
+
539
+ }
540
+
541
+ }
542
+
543
+
544
+
545
+ DataGrid_CSV.ItemsSource = table.DefaultView;
546
+
547
+ }
548
+
549
+ private void Button_Click(object sender, RoutedEventArgs e)
550
+
551
+ {
552
+
553
+ for(int i = 1; i <= 5; i++)
554
+
555
+ {
556
+
557
+ string path_write = @"new_Param_" + i.ToString("000") + ".csv";
558
+
559
+
560
+
561
+ // 名前と値のペアのリストを作る
562
+
563
+ var list_file = table.AsEnumerable()
564
+
565
+ .Select(x => new object[] { x[0], x[i] })
566
+
567
+ .ToList();
568
+
569
+
570
+
571
+ FileWrite(path_write, list_file);
572
+
573
+ }
574
+
575
+ }
576
+
577
+
578
+
579
+ public void FileRead(string path_read, List<string[]> list_file)
580
+
581
+ {
582
+
583
+ try
584
+
585
+ {
586
+
587
+ using(var sr = new StreamReader(path_read, Encoding.GetEncoding("shift_jis")))
588
+
589
+ {
590
+
591
+ while(!sr.EndOfStream)
592
+
593
+ {
594
+
595
+ string line = sr.ReadLine();
596
+
597
+ string[] csv_array = line.Split(',');
598
+
599
+ list_file.Add(csv_array);
600
+
601
+ }
602
+
603
+ }
604
+
605
+ }
606
+
607
+ catch(Exception e) { }
608
+
609
+ }
610
+
611
+
612
+
613
+ public void FileWrite(string path_write, List<object[]> list_file)
614
+
615
+ {
616
+
617
+ using(var sw = new StreamWriter(path_write, false, Encoding.GetEncoding("shift_jis")))
618
+
619
+ {
620
+
621
+ foreach(object[] csv_array in list_file)
622
+
623
+ {
624
+
625
+ sw.WriteLine(string.Join(",", csv_array));
626
+
627
+ }
628
+
629
+ }
630
+
631
+ }
632
+
633
+ }
634
+
635
+ }
636
+
637
+ ```