DataGridView Custom DataGridViewColumn单元格的奇怪字符'q'问题

时间:2011-01-24 15:12:09

标签: c# winforms datagridview datagridviewcolumn

我为datagridview创建了一个自定义列,从DataGridViewColumn,DataGridViewTextBoxCell派生,并实现了IDataGridViewEditingControl。

我创建了一个包含简单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与任何DataSource都没有关联。

有人能指出我在这段代码中遇到的问题吗?

2 个答案:

答案 0 :(得分:2)

尝试放置此代码并查看行为。 (请参阅我的问题下面的评论)

你说的问题..我可以使用箭头键重新制作 - >导航到单元格。 问题是只有在按下第一个键后才进入编辑模式。 尝试将此代码放在CustomTextCell类型中并查看行为。

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

答案 1 :(得分: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!
      }
    }