回答編集履歴
1
ソース部分をマークダウン
test
CHANGED
@@ -1,16 +1,24 @@
|
|
1
1
|
以下のように、列名を望む順番に並べたstring配列を作成し、添え字を振ってみてはどうでしょう。
|
2
|
+
|
3
|
+
```C#
|
2
4
|
|
3
5
|
foreach(var colNameIndex in new string[] { "Name", "Sex", "Age", "Like" }.Select((str, index) => new { str, index }))
|
4
6
|
|
5
|
-
|
7
|
+
{
|
6
8
|
|
7
|
-
|
9
|
+
this.dataGridView1.Columns[colNameIndex.str].DisplayIndex = colNameIndex.index;
|
8
10
|
|
9
|
-
|
11
|
+
}
|
12
|
+
|
13
|
+
```
|
10
14
|
|
11
15
|
または
|
12
16
|
|
17
|
+
```C#
|
18
|
+
|
13
19
|
new string[] { "Name", "Sex", "Age", "Like" }.Select((str, index) => new { str, index }).ToList().ForEach(colNameIndex => this.dataGridView1.Columns[colNameIndex.str].DisplayIndex = colNameIndex.index);
|
20
|
+
|
21
|
+
```
|
14
22
|
|
15
23
|
|
16
24
|
|
@@ -20,16 +28,24 @@
|
|
20
28
|
|
21
29
|
列幅は、まとめて指定なら(例として20を指定)
|
22
30
|
|
23
|
-
|
31
|
+
```C#
|
24
32
|
|
25
|
-
|
33
|
+
foreach(DataGridViewColumn col in this.dataGridView1.Columns)
|
26
34
|
|
27
|
-
|
35
|
+
{
|
28
36
|
|
37
|
+
col.Width = 20;
|
38
|
+
|
29
|
-
|
39
|
+
}
|
40
|
+
|
41
|
+
```
|
30
42
|
|
31
43
|
もしくは
|
32
44
|
|
45
|
+
```C#
|
46
|
+
|
33
47
|
this.dataGridView1.Columns.Cast<DataGridViewColumn>().ToList().ForEach(col=>col.Width=20);
|
34
48
|
|
49
|
+
|
50
|
+
|
35
|
-
で良いと思います。
|
51
|
+
```で良いと思います。
|