2011-01-24 21 views
2

DataGridViewColumn、DataGridViewTextBoxCellから派生し、IDataGridViewEditingControlを実装するデータグリッドビューのカスタム列を作成しました。DataGridViewカスタムの奇妙な文字 'q'の問題カスタムDataGridViewColumnセル

シンプルなTextBoxを含むユーザーコントロールを作成しました。このコントロールをそのコントロールの編集コントロールとして使用しました。

今、私はこのシナリオで奇妙な問題を抱えています。文字 'q'を押すと、DataGridViewのカスタム列のセルには表示されず何も起こりませんが、他のキーを押すとセルに正しく表示されます。

ここで問題が発生する可能性がありますか?私はすべてのイベントをデバッグすることであらゆる方法を試しましたが、何も見つかりませんでした。

私が作成したカスタム列の下のコードを与えています。

public class CustomControlColumn : DataGridViewColumn 
    { 
    public CustomControlColumn() : base(new CustomTextCell()) { } 

    public override DataGridViewCell CellTemplate 
    { 
     get 
     { 
     return base.CellTemplate; 
     } 
     set 
     { 
     // Ensure that the cell used for the template is a CustomComboCell. 
     if (value != null && !value.GetType().IsAssignableFrom(typeof(CustomTextCell))) 
     { 
      throw new InvalidCastException("Must be a CustomTextCell"); 
     } 

     base.CellTemplate = value; 
     } 
    } 
    } 

    public class CustomTextCell : DataGridViewTextBoxCell 
    { 
    public override void InitializeEditingControl(int rowIndex, object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) 
    { 
     // Set the value of the editing control to the current cell value. 
     base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle); 
     var control = DataGridView.EditingControl as CustomTextBoxControl; 

     if (OwningColumn == null) 
     return; 

     // Use the default row value when Value property is null. 
     if (Value == null) 
     { 
     control.Text = string.Empty; 
     } 
     else 
     { 
     control.Text = Value.ToString(); 
     } 
    } 

    public override Type EditType 
    { 
     get 
     { 
     // Return the type of the editing contol that CustomComboCell uses. 
     return typeof(CustomControlEditingControl); 
     } 
    } 

    public override Type ValueType 
    { 
     get 
     { 
     // Return the type of the value that CustomTextCell contains. 
     return typeof(String); 
     } 
    } 

    public override object DefaultNewRowValue 
    { 
     get 
     { 
     // Use the empty string as the default value. 
     return string.Empty; 
     } 
    } 
    } 

    class CustomControlEditingControl : CustomTextBoxControl, IDataGridViewEditingControl 
    { 
    public void ApplyCellStyleToEditingControl(DataGridViewCellStyle dataGridViewCellStyle) 
    { 
     // Nothing to do 
    } 

    public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey) 
    { 
     // Let the control handle the keys listed. 
     switch (keyData & Keys.KeyCode) 
     { 
     case Keys.Left: 
     case Keys.Up: 
     case Keys.Down: 
     case Keys.Right: 
     case Keys.Home: 
     case Keys.End: 
     case Keys.PageDown: 
     case Keys.PageUp: 
      return true; 
     default: 
      return false; 
     } 
    } 

    public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts context) 
    { 
     return EditingControlFormattedValue; 
    } 

    public DataGridView EditingControlDataGridView { get; set; } 

    public object EditingControlFormattedValue 
    { 
     get { return Text; } 
     set { Text = value.ToString(); } 
    } 

    public int EditingControlRowIndex { get; set;} 

    public bool EditingControlValueChanged { get; set; } 

    public Cursor EditingPanelCursor 
    { 
     get { return base.Cursor; } 
    } 

    public bool RepositionEditingControlOnValueChange 
    { 
     get { return false; } 
    } 
    } 

デザイナーとのDataGridView通じ、私のDataGridViewの列のいずれかが任意のデータソースに関連付けられていないように私はこれを追加しました。

誰も私にこのコードの問題を指摘できますか?

+0

は、「Q」は、これまでのボックスに表示されません...か? – curtisk

+0

@curtisk:どこかからコピーして貼り付けると表示されません。またShift + qを押すと、大文字の「Q」として表示されます。私はこの行動に非常に驚いています。 – JPReddy

+0

「q」キーだけではありません。矢印キーを使用してセルに移動してキーを押すと、そのキー入力は無視されます。それ以外の場合は正常に動作します。 'q'と他のキー入力が必要です。 –

答えて

2

は、以下の変更を行い、それが助けかどうかを確認:あなたが最初にそれを押した場合にのみ、

public bool EditingControlWantsInputKey(Keys keyData, bool dataGridViewWantsInputKey) 
    { 
     // Let the control handle the keys listed. 
     switch (keyData & Keys.KeyCode) 
     { 
     case Keys.Left: 
     case Keys.Up: 
     case Keys.Down: 
     case Keys.Right: 
     case Keys.Home: 
     case Keys.End: 
     case Keys.PageDown: 
     case Keys.PageUp: 
      return true; 
     default: 
     // return false; <--not this 
     return !dataGridViewWantsInputKey; //try this! 
     } 
    } 
+0

これは完璧なカルトディスクです。それは雄牛目だった。私はVijay Sirigiriによって与えられた説明も読んだ。どうもありがとう。 – JPReddy

+0

MSDNを見ても、コードサンプルが2.0から4.0まで変化していても、私はこの奇妙な答えを見つけることができてうれしいです – curtisk

+0

本当にとても奇妙なことに、私は最後からそれの理由を見つけようとしていました4時間。 – JPReddy

2

このコードを入力して、動作を確認してください。 (あなたの質問の下に私のコメントを参照してください)

あなたが言った問題..私は矢印キーを使用してreproできる - >セルにナビゲートする。 問題は、最初のキーを押した後にのみモードを編集することになります。 このコードをCustomTextCell型に配置して、その動作を確認してください。

public override bool KeyEntersEditMode(KeyEventArgs e) 
{ 
    // for testing..  
    return true; 
} 
+0

ポストに感謝します。私のアプリでは、DGVはすでにEditOnEnterプロパティをtrueにしています。そのセルをクリックすると、編集モードに入り、他のキーを押すと表示されますが、最初のキーであるかどうかにかかわらず、qは表示されません。この振る舞いで完全に困惑した。他の文字を入力してもqキーが表示されますか? – JPReddy

+0

はい。しかし、私が言及したように、それはどのキーストロークでも初めて起こります。私はその物件を設定しようとし、何が起こるか見る。しかし、私が投稿したコードを追加した後、うまくいきます。 –

+0

「q」文字がサンプルアプリケーションに表示されることはありません。しかし、シフト+ qを使用すると、最初のキーを押しても表示されます。 :( – JPReddy