C#でDataGridViewの列にNumericUpDownにしたくカスタマイズを行っています。
http://msdn.microsoft.com/ja-jp/library/7tas5c80 …
こちらのサイトを参考にし以下のソースになりました。
public class NumericUpDownColumn : DataGridViewColumn
{
public NumericUpDownColumn()
: base(new NumericUpDownCell())
{
}
public override DataGridViewCell CellTemplate
{
get
{
return base.CellTemplate;
}
set
{
base.CellTemplate = value;
}
}
}
public class NumericUpDownCell : DataGridViewTextBoxCell
{
public override void InitializeEditingControl(int rowIndex, object
initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue,
dataGridViewCellStyle);
NumericUpDownCellEditingControl ctl =
DataGridView.EditingControl as NumericUpDownCellEditingControl;
if (this.Value != null) ctl.Value = decimal.Parse(this.Value.ToString());
}
public override Type EditType
{
get
{
return typeof(NumericUpDownCellEditingControl);
}
}
public override Type ValueType
{
get
{
return typeof(decimal);
}
}
public override object DefaultNewRowValue
{
get
{
return (decimal)0;
}
}
}
class NumericUpDownCellEditingControl : NumericUpDown, IDataGridViewEditingControl
{
DataGridView dataGridView;
private bool valueChanged = false;
int rowIndex;
public object EditingControlFormattedValue
{
get
{
return this.Value;
}
set
{
String newValue = value as String;
if (newValue != null)
{
this.Value = decimal.Parse(newValue);
}
}
}
public object GetEditingControlFormattedValue(
DataGridViewDataErrorContexts context)
{
return EditingControlFormattedValue;
}
public void ApplyCellStyleToEditingControl(
DataGridViewCellStyle dataGridViewCellStyle)
{
this.Font = dataGridViewCellStyle.Font;
}
public int EditingControlRowIndex
{
get
{
return rowIndex;
}
set
{
rowIndex = value;
}
}
public bool EditingControlWantsInputKey(
Keys key, bool dataGridViewWantsInputKey)
{
switch (key & Keys.KeyCode)
{
case Keys.Left:
case Keys.Up:
case Keys.Down:
case Keys.Right:
return true;
default:
return false;
}
}
public bool RepositionEditingControlOnValueChange
{
get
{
return false;
}
}
public DataGridView EditingControlDataGridView
{
get
{
return dataGridView;
}
set
{
dataGridView = value;
}
}
public bool EditingControlValueChanged
{
get
{
return valueChanged;
}
set
{
valueChanged = value;
}
}
public Cursor EditingPanelCursor
{
get
{
return base.Cursor;
}
}
protected override void OnValueChanged(EventArgs eventargs)
{
valueChanged = true;
this.EditingControlDataGridView.NotifyCurrentCellDirty(true);
base.OnValueChanged(eventargs);
}
}
}
デザイン画面で列を追加し実行。グリッド上のNumericUpDownで数値を変更し
違うセルを選択すると「セルのフォーマットされた値に間違った型が指定されていますと例外がでます。場所はDataGridViewCellのParseFormattedValueでした。
どのようにすれば例外がでなくなるでしょうか。ちょっと文字数の関係上色々はしょってしまってます。
よろしくお願いします。
No.1ベストアンサー
- 回答日時:
NumericUpDownCell の ParseFormattedValue をオーバーライドしてやればよいと思います。
デフォルトだと
public override object ParseFormattedValue(object formattedValue, DataGridViewCellStyle cellStyle, System.ComponentModel.TypeConverter formattedValueTypeConverter, System.ComponentModel.TypeConverter valueTypeConverter)
{
return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
}
のようになると思いますが、とりあえず、
return base.ParseFormattedValue(formattedValue.ToString(), cellStyle, formattedValueTypeConverter, valueTypeConverter);
のようにすると例外は回避できると思います。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
このQ&Aを見た人はこんなQ&Aも見ています
-
初めて自分の家と他人の家が違う、と意識した時
子供の頃、友達の家に行くと「なんか自分の家と匂いが違うな?」って思いませんでしたか?
-
フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
あなたが普段思っている「これまだ誰も言ってなかったけど共感されるだろうな」というあるあるを教えてください
-
映画のエンドロール観る派?観ない派?
映画が終わった後、すぐに席を立って帰る方もちらほら見かけます。皆さんはエンドロールの最後まで観ていきますか?
-
海外旅行から帰ってきたら、まず何を食べる?
帰国して1番食べたくなるもの、食べたくなるだろうなと思うもの、皆さんはありますか?
-
天使と悪魔選手権
悪魔がこんなささやきをしていたら、天使のあなたはなんと言って止めますか?
-
VB2005EE:DataGridViewでチェックボックスを設けるには
Visual Basic(VBA)
-
「指定されたキャストは有効ではありません。」とエラーが出てしまいます。
Microsoft ASP
-
CloseとDisposeの違い
Visual Basic(VBA)
-
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・人生のプチ美学を教えてください!!
- ・10秒目をつむったら…
- ・あなたの習慣について教えてください!!
- ・牛、豚、鶏、どれか一つ食べられなくなるとしたら?
- ・【大喜利】【投稿~9/18】 おとぎ話『桃太郎』の知られざるエピソード
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
このQ&Aを見た人がよく見るQ&A
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
大量のデータを読み込んで表示...
-
Junitテストでvoid戻り値メッソ...
-
コンボボックスを使う時の警告
-
C#で、あるクラスのメンバーす...
-
C# タスク非表示でタイトルバー...
-
C# タスクバーを隠したフォーム...
-
Runtime#proccessのwait
-
GetDIBits関数の使い方について
-
C#でキーイベントが発生しない...
-
C# DataGridView列カスタマイズ
-
vc++ 2005 Pictureboxの透過処理
-
doja5.0 指定した時刻までのカ...
-
JavaFXでの表内のチェックボッ...
-
C#でのWNetAddConnection3の使...
-
ユーザーコントロールを動的に...
-
C#で別のFormへ複数の値を返そ...
-
ボタンの複数割り当てについて
-
DocumentListenerについて
-
struts2のchainで値の受け渡し...
-
インターフェイス(独習JAVAのP...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
大量のデータを読み込んで表示...
-
C#で、あるクラスのメンバーす...
-
C#でのWNetAddConnection3の使...
-
Junitテストでvoid戻り値メッソ...
-
ボタンの複数割り当てについて
-
C# DataGridView列カスタマイズ
-
C# WinForm のDataGridView Cel...
-
C# visibleプロパティをfalseに...
-
[C#.net]スレッド化された別フ...
-
C# 矢印キーの取得
-
unityでのC++エラーの原因がわ...
-
C#から、C++で作成したdll内の...
-
コンボボックスを使う時の警告
-
JavaFXでの表内のチェックボッ...
-
C#で別のFormへ複数の値を返そ...
-
vc++ 2005 Pictureboxの透過処理
-
プログラミング(Java)じゃん...
-
C#でキーイベントが発生しない...
-
正規表現
-
C# MouseHoverを何度も呼ぶには
おすすめ情報