質問するログイン新規登録

回答編集履歴

1

コード修正

2017/10/13 23:55

投稿

macchi123jp
macchi123jp

スコア141

answer CHANGED
@@ -9,17 +9,13 @@
9
9
  ' フォームロード時の処理
10
10
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
11
11
 
12
- '何かデータ
12
+ 'グリッド定義
13
13
  DataGridView1.ColumnCount = 2
14
+ DataGridView1.Columns(0).HeaderText = "名前"
14
- DataGridView1.RowCount = 3
15
+ DataGridView1.Columns(0).Name = "名前"
16
+ DataGridView1.Columns(1).HeaderText = "個数"
17
+ DataGridView1.Columns(1).Name = "個数"
15
18
 
16
- DataGridView1(0, 0).Value = "aaa"
17
- DataGridView1(1, 0).Value = 3
18
- DataGridView1(0, 1).Value = "bbb"
19
- DataGridView1(1, 1).Value = 4
20
- DataGridView1(0, 2).Value = "ccc"
21
- DataGridView1(1, 2).Value = 5
22
-
23
19
  Chart1.Series.Clear()
24
20
 
25
21
  End Sub
@@ -39,21 +35,13 @@
39
35
 
40
36
 
41
37
  '★★★データの追加★★★
38
+ For i = 0 To DataGridView1.Rows.Count - 2
42
- dtRow = ds.Tables(0).NewRow
39
+ dtRow = ds.Tables(0).NewRow
43
- dtRow(0) = DataGridView1(0, 0).Value
40
+ dtRow(0) = DataGridView1(0, i).Value
44
- dtRow(1) = DataGridView1(1, 0).Value
41
+ dtRow(1) = DataGridView1(1, i).Value
45
- ds.Tables(0).Rows.Add(dtRow)
42
+ ds.Tables(0).Rows.Add(dtRow)
43
+ Next
46
44
 
47
- dtRow = ds.Tables(0).NewRow
48
- dtRow(0) = DataGridView1(0, 1).Value
49
- dtRow(1) = DataGridView1(1, 1).Value
50
- ds.Tables(0).Rows.Add(dtRow)
51
-
52
- dtRow = ds.Tables(0).NewRow
53
- dtRow(0) = DataGridView1(0, 2).Value
54
- dtRow(1) = DataGridView1(1, 2).Value
55
- ds.Tables(0).Rows.Add(dtRow)
56
-
57
45
  Return (ds)
58
46
 
59
47
  End Function
@@ -70,6 +58,7 @@
70
58
  Chart1.Series.Clear()
71
59
 
72
60
  'Chartコントロールにタイトルを設定
61
+ Chart1.Titles.Clear()
73
62
  Chart1.Titles.Add("アクセス数とユニークユーザー数")
74
63
 
75
64
  'グラフの種類,系列,軸の設定
@@ -101,5 +90,31 @@
101
90
 
102
91
  End Sub
103
92
 
93
+ Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
94
+ '表示されているコントロールがDataGridViewTextBoxEditingControlか調べる
95
+ If TypeOf e.Control Is DataGridViewTextBoxEditingControl Then
96
+ Dim dgv As DataGridView = CType(sender, DataGridView)
97
+
98
+ '編集のために表示されているコントロールを取得
99
+ Dim tb As DataGridViewTextBoxEditingControl =
100
+ CType(e.Control, DataGridViewTextBoxEditingControl)
101
+
102
+ 'イベントハンドラを削除
103
+ RemoveHandler tb.KeyPress, AddressOf DataGridView1_KeyPress
104
+
105
+ '該当する列か調べる
106
+ If dgv.CurrentCell.OwningColumn.Name = "個数" Then
107
+ 'KeyPressイベントハンドラを追加
108
+ AddHandler tb.KeyPress, AddressOf DataGridView1_KeyPress
109
+ End If
110
+ End If
111
+ End Sub
112
+
113
+ Private Sub DataGridView1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles DataGridView1.KeyPress
114
+ '数字しか入力できないようにする
115
+ If e.KeyChar < "0"c Or e.KeyChar > "9"c Then
116
+ e.Handled = True
117
+ End If
118
+ End Sub
104
119
  End Class
105
120
  ```