回答編集履歴

2

追記

2018/07/14 10:15

投稿

YAmaGNZ
YAmaGNZ

スコア10222

test CHANGED
@@ -1,3 +1,447 @@
1
1
  DataGridViewのColumnsはDataGridViewColumnCollectionでそのItemはDataGridViewColumnとなります。
2
2
 
3
3
  ですので、型をチェックし、DataGridViewTextBoxColumnEXであればキャストしてください。
4
+
5
+
6
+
7
+ ### 追記
8
+
9
+ DataGridViewCell、編集コントロールの拡張を行う場合
10
+
11
+ (編集コントロールでの数値入力のみ許可する部分は実装してありません。TextBoxを継承していますので、検索すればいろいろ方法が引っかかると思います。)
12
+
13
+ このようにすれば、通常のDataGridViewからでも使えます。
14
+
15
+ ```C#
16
+
17
+ /// <summary>
18
+
19
+ /// DataGridViewTextBoxCellEXオブジェクトの列を表します。
20
+
21
+ /// </summary>
22
+
23
+ public class DataGridViewTextBoxColumnEX : DataGridViewColumn
24
+
25
+ {
26
+
27
+ //CellTemplateとするDataGridViewMaskedTextBoxCellオブジェクトを指定して基本クラスのコンストラクタを呼び出す
28
+
29
+ public DataGridViewTextBoxColumnEX() : base(new DataGridViewTextBoxCellEX())
30
+
31
+ {
32
+
33
+ }
34
+
35
+
36
+
37
+ public Boolean 数値入力 { get; set; }
38
+
39
+
40
+
41
+ //新しいプロパティを追加しているため、Cloneメソッドをオーバーライドする必要がある
42
+
43
+ public override object Clone()
44
+
45
+ {
46
+
47
+ DataGridViewTextBoxColumnEX col = (DataGridViewTextBoxColumnEX)base.Clone();
48
+
49
+ col.数値入力 = this.数値入力;
50
+
51
+ return col;
52
+
53
+ }
54
+
55
+
56
+
57
+ //CellTemplateの取得と設定
58
+
59
+ public override DataGridViewCell CellTemplate
60
+
61
+ {
62
+
63
+ get
64
+
65
+ {
66
+
67
+ return base.CellTemplate;
68
+
69
+ }
70
+
71
+ set
72
+
73
+ {
74
+
75
+ // DataGridViewTextBoxCellEX以外はCellTemplateに設定できないようにする
76
+
77
+ if (!(value is DataGridViewTextBoxCellEX))
78
+
79
+ {
80
+
81
+ throw new InvalidCastException("DataGridViewMaskedTextBoxCellオブジェクトを指定してください。");
82
+
83
+ }
84
+
85
+ base.CellTemplate = value;
86
+
87
+ }
88
+
89
+ }
90
+
91
+ }
92
+
93
+
94
+
95
+ public class DataGridViewTextBoxCellEX : DataGridViewTextBoxCell
96
+
97
+ {
98
+
99
+
100
+
101
+ //編集コントロールを初期化する
102
+
103
+ //編集コントロールは別のセルや列でも使いまわされるため、初期化の必要がある
104
+
105
+ public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
106
+
107
+ {
108
+
109
+ base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
110
+
111
+
112
+
113
+ //編集コントロールの取得
114
+
115
+ TextBoxEX textEX = this.DataGridView.EditingControl as TextBoxEX;
116
+
117
+ if (textEX != null)
118
+
119
+ {
120
+
121
+ //Textを設定
122
+
123
+ string setText = initialFormattedValue as string;
124
+
125
+ textEX.Text = setText != null ? setText : "";
126
+
127
+
128
+
129
+ //カスタム列のプロパティを反映させる
130
+
131
+ DataGridViewTextBoxColumnEX column = this.OwningColumn as DataGridViewTextBoxColumnEX;
132
+
133
+ if (column != null)
134
+
135
+ {
136
+
137
+ textEX.数値入力 = column.数値入力;
138
+
139
+ }
140
+
141
+ }
142
+
143
+ }
144
+
145
+
146
+
147
+ //編集コントロールの型を指定する
148
+
149
+ public override Type EditType
150
+
151
+ {
152
+
153
+ get
154
+
155
+ {
156
+
157
+ return typeof(TextBoxEX);
158
+
159
+ }
160
+
161
+ }
162
+
163
+
164
+
165
+ }
166
+
167
+
168
+
169
+ public partial class TextBoxEX : TextBox, IDataGridViewEditingControl
170
+
171
+ {
172
+
173
+
174
+
175
+ #region プロパティ
176
+
177
+
178
+
179
+ public Boolean 数値入力 { get; set; }
180
+
181
+
182
+
183
+ #endregion
184
+
185
+
186
+
187
+ //コンストラクタ
188
+
189
+ public TextBoxEX()
190
+
191
+ {
192
+
193
+ this.TabStop = false;
194
+
195
+ }
196
+
197
+
198
+
199
+ #region IDataGridViewEditingControl メンバ
200
+
201
+
202
+
203
+ //編集コントロールで変更されたセルの値
204
+
205
+ public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
206
+
207
+ {
208
+
209
+ return this.Text;
210
+
211
+ }
212
+
213
+
214
+
215
+ //編集コントロールで変更されたセルの値
216
+
217
+ public object EditingControlFormattedValue
218
+
219
+ {
220
+
221
+ get
222
+
223
+ {
224
+
225
+ return this.GetEditingControlFormattedValue(DataGridViewDataErrorContexts.Formatting);
226
+
227
+ }
228
+
229
+ set
230
+
231
+ {
232
+
233
+ this.Text = (string)value;
234
+
235
+ }
236
+
237
+ }
238
+
239
+
240
+
241
+ //セルスタイルを編集コントロールに適用する
242
+
243
+ //編集コントロールの前景色、背景色、フォントなどをセルスタイルに合わせる
244
+
245
+ public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
246
+
247
+ {
248
+
249
+ this.Font = dataGridViewCellStyle.Font;
250
+
251
+ this.ForeColor = dataGridViewCellStyle.ForeColor;
252
+
253
+ this.BackColor = dataGridViewCellStyle.BackColor;
254
+
255
+
256
+
257
+ switch (dataGridViewCellStyle.Alignment)
258
+
259
+ {
260
+
261
+ case DataGridViewContentAlignment.BottomCenter:
262
+
263
+ case DataGridViewContentAlignment.MiddleCenter:
264
+
265
+ case DataGridViewContentAlignment.TopCenter:
266
+
267
+ this.TextAlign = HorizontalAlignment.Center;
268
+
269
+ break;
270
+
271
+ case DataGridViewContentAlignment.BottomRight:
272
+
273
+ case DataGridViewContentAlignment.MiddleRight:
274
+
275
+ case DataGridViewContentAlignment.TopRight:
276
+
277
+ this.TextAlign = HorizontalAlignment.Right;
278
+
279
+ break;
280
+
281
+ default:
282
+
283
+ this.TextAlign = HorizontalAlignment.Left;
284
+
285
+ break;
286
+
287
+ }
288
+
289
+ }
290
+
291
+
292
+
293
+ //編集するセルがあるDataGridView
294
+
295
+ public DataGridView EditingControlDataGridView { get; set; }
296
+
297
+
298
+
299
+ //編集している行のインデックス
300
+
301
+ public int EditingControlRowIndex { get; set; }
302
+
303
+
304
+
305
+ //値が変更されたかどうか
306
+
307
+ //編集コントロールの値とセルの値が違うかどうか
308
+
309
+ public bool EditingControlValueChanged { get; set; }
310
+
311
+
312
+
313
+ //指定されたキーをDataGridViewが処理するか、編集コントロールが処理するか
314
+
315
+ //Trueを返すと、編集コントロールが処理する
316
+
317
+ //dataGridViewWantsInputKeyがTrueの時は、DataGridViewが処理できる
318
+
319
+ public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
320
+
321
+ {
322
+
323
+ //Keys.Left、Right、Home、Endの時は、Trueを返す
324
+
325
+ //このようにしないと、これらのキーで別のセルにフォーカスが移ってしまう
326
+
327
+ switch (keyData & Keys.KeyCode)
328
+
329
+ {
330
+
331
+ case Keys.Right:
332
+
333
+ case Keys.End:
334
+
335
+ case Keys.Left:
336
+
337
+ case Keys.Home:
338
+
339
+ return true;
340
+
341
+ default:
342
+
343
+ return !dataGridViewWantsInputKey;
344
+
345
+ }
346
+
347
+ }
348
+
349
+
350
+
351
+ //マウスカーソルがEditingPanel上にあるときのカーソルを指定する
352
+
353
+ public Cursor EditingPanelCursor
354
+
355
+ {
356
+
357
+ get
358
+
359
+ {
360
+
361
+ return base.Cursor;
362
+
363
+ }
364
+
365
+ }
366
+
367
+
368
+
369
+ //コントロールで編集する準備をする
370
+
371
+ //テキストを選択状態にしたり、挿入ポインタを末尾にしたりする
372
+
373
+ public void PrepareEditingControlForEdit(bool selectAll)
374
+
375
+ {
376
+
377
+ if (selectAll)
378
+
379
+ {
380
+
381
+ //選択状態にする
382
+
383
+ this.SelectAll();
384
+
385
+ }
386
+
387
+ else
388
+
389
+ {
390
+
391
+ //挿入ポインタを末尾にする
392
+
393
+ this.SelectionStart = this.TextLength;
394
+
395
+ }
396
+
397
+ }
398
+
399
+
400
+
401
+ //値が変更した時に、セルの位置を変更するかどうか
402
+
403
+ //値が変更された時に編集コントロールの大きさが変更される時はTrue
404
+
405
+ public bool RepositionEditingControlOnValueChange
406
+
407
+ {
408
+
409
+ get
410
+
411
+ {
412
+
413
+ return false;
414
+
415
+ }
416
+
417
+ }
418
+
419
+
420
+
421
+ #endregion
422
+
423
+
424
+
425
+ //値が変更された時
426
+
427
+ protected override void OnTextChanged(EventArgs e)
428
+
429
+ {
430
+
431
+ base.OnTextChanged(e);
432
+
433
+ //値が変更されたことをDataGridViewに通知する
434
+
435
+ EditingControlValueChanged = true;
436
+
437
+ EditingControlDataGridView.NotifyCurrentCellDirty(true);
438
+
439
+ }
440
+
441
+
442
+
443
+ }
444
+
445
+ }
446
+
447
+ ```

1

修正

2018/07/14 10:15

投稿

YAmaGNZ
YAmaGNZ

スコア10222

test CHANGED
@@ -1,3 +1,3 @@
1
1
  DataGridViewのColumnsはDataGridViewColumnCollectionでそのItemはDataGridViewColumnとなります。
2
2
 
3
- ですので、DataGridViewTextBoxColumnEXキャストしてください。
3
+ ですので、型をチェックし、DataGridViewTextBoxColumnEXであればキャストしてください。