2

DataGridViewColumn、DataGridViewTextBoxCell から派生させ、IDataGridViewEditingControl を実装して、datagridview のカスタム列を作成しました。

シンプルな 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 の列の 1 つとして追加しましたが、DataGridView はどの DataSource にも関連付けられていません。

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

4

2 に答える 2

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!
      }
    }
于 2011-01-24T16:09:36.120 に答える
2

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

あなたが述べた問題..矢印キーを使用して再現できました->セルに移動します。問題は、最初のキーが押された後にのみ編集モードになることです。このコードを CustomTextCell 型に配置して、動作を確認してください。

 public override bool KeyEntersEditMode(KeyEventArgs e)
 {
     // for testing..     
     return true;
 }
于 2011-01-24T15:57:12.323 に答える