奇怪的角色'q' DataGridView 自定义 DataGridViewColumn 单元格的问题

发布于 2024-10-13 15:02:36 字数 3612 浏览 6 评论 0原文

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

我创建了一个包含简单文本框的用户控件,并使用该控件作为该列的编辑控件。

现在我在这种情况下遇到了一个奇怪的问题。当我按字符“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 不与任何数据源关联。

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

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
    {
      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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗心人 2024-10-20 15:02:37

进行以下更改,看看是否有帮助:

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

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!
      }
    }
愛放△進行李 2024-10-20 15:02:37

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

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

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

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;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文