开发者

Weird character 'q' problem with DataGridView Custom DataGridViewColumn cell

I've created a custom column for the datagridview deriving it from DataGridViewColumn, DataGridViewTextBoxCell and implementing the IDataGridViewEditingControl.

I've created a user control which contains a simple TextBox and I used this control as editing control for that column.

Now I'm getting a weird problem with this scenario. When I press character 'q', the cell in the custom column of DataGridView doesn't display it and nothing happens, but when I press any other key it properly displays in the cell.

What could be the issue here? I tried all kind of ways by debugging it for every event but in vain I couldn't find anything yet.

I'm giving the code below of custom column I created.

  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
  开发者_JAVA百科  {
      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; }
    }
  }

I've added this as one of the column in my DataGridView through designer and DataGridView is not associated with any DataSource.

Can anybody point me towards the problem in this code?


Make the below change and see if that helps:

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!
      }
    }


Try to put this code and see the behavior. (see my comments below your question)

The problem you stated.. I could repro by using the arrow keys -> navigating to the cell. The issue is it goes to edit mode only after a first key is pressed. Try to put this code in CustomTextCell type and see the behavior.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜