回答編集履歴
2
追記
answer
CHANGED
@@ -1,2 +1,224 @@
|
|
1
1
|
DataGridViewのColumnsはDataGridViewColumnCollectionでそのItemはDataGridViewColumnとなります。
|
2
|
-
ですので、型をチェックし、DataGridViewTextBoxColumnEXであればキャストしてください。
|
2
|
+
ですので、型をチェックし、DataGridViewTextBoxColumnEXであればキャストしてください。
|
3
|
+
|
4
|
+
### 追記
|
5
|
+
DataGridViewCell、編集コントロールの拡張を行う場合
|
6
|
+
(編集コントロールでの数値入力のみ許可する部分は実装してありません。TextBoxを継承していますので、検索すればいろいろ方法が引っかかると思います。)
|
7
|
+
このようにすれば、通常のDataGridViewからでも使えます。
|
8
|
+
```C#
|
9
|
+
/// <summary>
|
10
|
+
/// DataGridViewTextBoxCellEXオブジェクトの列を表します。
|
11
|
+
/// </summary>
|
12
|
+
public class DataGridViewTextBoxColumnEX : DataGridViewColumn
|
13
|
+
{
|
14
|
+
//CellTemplateとするDataGridViewMaskedTextBoxCellオブジェクトを指定して基本クラスのコンストラクタを呼び出す
|
15
|
+
public DataGridViewTextBoxColumnEX() : base(new DataGridViewTextBoxCellEX())
|
16
|
+
{
|
17
|
+
}
|
18
|
+
|
19
|
+
public Boolean 数値入力 { get; set; }
|
20
|
+
|
21
|
+
//新しいプロパティを追加しているため、Cloneメソッドをオーバーライドする必要がある
|
22
|
+
public override object Clone()
|
23
|
+
{
|
24
|
+
DataGridViewTextBoxColumnEX col = (DataGridViewTextBoxColumnEX)base.Clone();
|
25
|
+
col.数値入力 = this.数値入力;
|
26
|
+
return col;
|
27
|
+
}
|
28
|
+
|
29
|
+
//CellTemplateの取得と設定
|
30
|
+
public override DataGridViewCell CellTemplate
|
31
|
+
{
|
32
|
+
get
|
33
|
+
{
|
34
|
+
return base.CellTemplate;
|
35
|
+
}
|
36
|
+
set
|
37
|
+
{
|
38
|
+
// DataGridViewTextBoxCellEX以外はCellTemplateに設定できないようにする
|
39
|
+
if (!(value is DataGridViewTextBoxCellEX))
|
40
|
+
{
|
41
|
+
throw new InvalidCastException("DataGridViewMaskedTextBoxCellオブジェクトを指定してください。");
|
42
|
+
}
|
43
|
+
base.CellTemplate = value;
|
44
|
+
}
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
public class DataGridViewTextBoxCellEX : DataGridViewTextBoxCell
|
49
|
+
{
|
50
|
+
|
51
|
+
//編集コントロールを初期化する
|
52
|
+
//編集コントロールは別のセルや列でも使いまわされるため、初期化の必要がある
|
53
|
+
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
|
54
|
+
{
|
55
|
+
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
|
56
|
+
|
57
|
+
//編集コントロールの取得
|
58
|
+
TextBoxEX textEX = this.DataGridView.EditingControl as TextBoxEX;
|
59
|
+
if (textEX != null)
|
60
|
+
{
|
61
|
+
//Textを設定
|
62
|
+
string setText = initialFormattedValue as string;
|
63
|
+
textEX.Text = setText != null ? setText : "";
|
64
|
+
|
65
|
+
//カスタム列のプロパティを反映させる
|
66
|
+
DataGridViewTextBoxColumnEX column = this.OwningColumn as DataGridViewTextBoxColumnEX;
|
67
|
+
if (column != null)
|
68
|
+
{
|
69
|
+
textEX.数値入力 = column.数値入力;
|
70
|
+
}
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
74
|
+
//編集コントロールの型を指定する
|
75
|
+
public override Type EditType
|
76
|
+
{
|
77
|
+
get
|
78
|
+
{
|
79
|
+
return typeof(TextBoxEX);
|
80
|
+
}
|
81
|
+
}
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
public partial class TextBoxEX : TextBox, IDataGridViewEditingControl
|
86
|
+
{
|
87
|
+
|
88
|
+
#region プロパティ
|
89
|
+
|
90
|
+
public Boolean 数値入力 { get; set; }
|
91
|
+
|
92
|
+
#endregion
|
93
|
+
|
94
|
+
//コンストラクタ
|
95
|
+
public TextBoxEX()
|
96
|
+
{
|
97
|
+
this.TabStop = false;
|
98
|
+
}
|
99
|
+
|
100
|
+
#region IDataGridViewEditingControl メンバ
|
101
|
+
|
102
|
+
//編集コントロールで変更されたセルの値
|
103
|
+
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context)
|
104
|
+
{
|
105
|
+
return this.Text;
|
106
|
+
}
|
107
|
+
|
108
|
+
//編集コントロールで変更されたセルの値
|
109
|
+
public object EditingControlFormattedValue
|
110
|
+
{
|
111
|
+
get
|
112
|
+
{
|
113
|
+
return this.GetEditingControlFormattedValue(DataGridViewDataErrorContexts.Formatting);
|
114
|
+
}
|
115
|
+
set
|
116
|
+
{
|
117
|
+
this.Text = (string)value;
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
//セルスタイルを編集コントロールに適用する
|
122
|
+
//編集コントロールの前景色、背景色、フォントなどをセルスタイルに合わせる
|
123
|
+
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle)
|
124
|
+
{
|
125
|
+
this.Font = dataGridViewCellStyle.Font;
|
126
|
+
this.ForeColor = dataGridViewCellStyle.ForeColor;
|
127
|
+
this.BackColor = dataGridViewCellStyle.BackColor;
|
128
|
+
|
129
|
+
switch (dataGridViewCellStyle.Alignment)
|
130
|
+
{
|
131
|
+
case DataGridViewContentAlignment.BottomCenter:
|
132
|
+
case DataGridViewContentAlignment.MiddleCenter:
|
133
|
+
case DataGridViewContentAlignment.TopCenter:
|
134
|
+
this.TextAlign = HorizontalAlignment.Center;
|
135
|
+
break;
|
136
|
+
case DataGridViewContentAlignment.BottomRight:
|
137
|
+
case DataGridViewContentAlignment.MiddleRight:
|
138
|
+
case DataGridViewContentAlignment.TopRight:
|
139
|
+
this.TextAlign = HorizontalAlignment.Right;
|
140
|
+
break;
|
141
|
+
default:
|
142
|
+
this.TextAlign = HorizontalAlignment.Left;
|
143
|
+
break;
|
144
|
+
}
|
145
|
+
}
|
146
|
+
|
147
|
+
//編集するセルがあるDataGridView
|
148
|
+
public DataGridView EditingControlDataGridView { get; set; }
|
149
|
+
|
150
|
+
//編集している行のインデックス
|
151
|
+
public int EditingControlRowIndex { get; set; }
|
152
|
+
|
153
|
+
//値が変更されたかどうか
|
154
|
+
//編集コントロールの値とセルの値が違うかどうか
|
155
|
+
public bool EditingControlValueChanged { get; set; }
|
156
|
+
|
157
|
+
//指定されたキーをDataGridViewが処理するか、編集コントロールが処理するか
|
158
|
+
//Trueを返すと、編集コントロールが処理する
|
159
|
+
//dataGridViewWantsInputKeyがTrueの時は、DataGridViewが処理できる
|
160
|
+
public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey)
|
161
|
+
{
|
162
|
+
//Keys.Left、Right、Home、Endの時は、Trueを返す
|
163
|
+
//このようにしないと、これらのキーで別のセルにフォーカスが移ってしまう
|
164
|
+
switch (keyData & Keys.KeyCode)
|
165
|
+
{
|
166
|
+
case Keys.Right:
|
167
|
+
case Keys.End:
|
168
|
+
case Keys.Left:
|
169
|
+
case Keys.Home:
|
170
|
+
return true;
|
171
|
+
default:
|
172
|
+
return !dataGridViewWantsInputKey;
|
173
|
+
}
|
174
|
+
}
|
175
|
+
|
176
|
+
//マウスカーソルがEditingPanel上にあるときのカーソルを指定する
|
177
|
+
public Cursor EditingPanelCursor
|
178
|
+
{
|
179
|
+
get
|
180
|
+
{
|
181
|
+
return base.Cursor;
|
182
|
+
}
|
183
|
+
}
|
184
|
+
|
185
|
+
//コントロールで編集する準備をする
|
186
|
+
//テキストを選択状態にしたり、挿入ポインタを末尾にしたりする
|
187
|
+
public void PrepareEditingControlForEdit(bool selectAll)
|
188
|
+
{
|
189
|
+
if (selectAll)
|
190
|
+
{
|
191
|
+
//選択状態にする
|
192
|
+
this.SelectAll();
|
193
|
+
}
|
194
|
+
else
|
195
|
+
{
|
196
|
+
//挿入ポインタを末尾にする
|
197
|
+
this.SelectionStart = this.TextLength;
|
198
|
+
}
|
199
|
+
}
|
200
|
+
|
201
|
+
//値が変更した時に、セルの位置を変更するかどうか
|
202
|
+
//値が変更された時に編集コントロールの大きさが変更される時はTrue
|
203
|
+
public bool RepositionEditingControlOnValueChange
|
204
|
+
{
|
205
|
+
get
|
206
|
+
{
|
207
|
+
return false;
|
208
|
+
}
|
209
|
+
}
|
210
|
+
|
211
|
+
#endregion
|
212
|
+
|
213
|
+
//値が変更された時
|
214
|
+
protected override void OnTextChanged(EventArgs e)
|
215
|
+
{
|
216
|
+
base.OnTextChanged(e);
|
217
|
+
//値が変更されたことをDataGridViewに通知する
|
218
|
+
EditingControlValueChanged = true;
|
219
|
+
EditingControlDataGridView.NotifyCurrentCellDirty(true);
|
220
|
+
}
|
221
|
+
|
222
|
+
}
|
223
|
+
}
|
224
|
+
```
|
1
修正
answer
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
DataGridViewのColumnsはDataGridViewColumnCollectionでそのItemはDataGridViewColumnとなります。
|
2
|
-
ですので、DataGridViewTextBoxColumnEX
|
2
|
+
ですので、型をチェックし、DataGridViewTextBoxColumnEXであればキャストしてください。
|