2011-01-24 135 views
2

我已经为datagridview从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与任何DataSource都没有关联。

任何人都可以指出我在这个代码中的问题?

+0

,那么“Q”曾经出现在中...或者只有当你按下它第一次? – curtisk 2011-01-24 15:43:26

+0

@ curtisk:它从来没有出现,当我从某处复制它并粘贴它显示。另外,如果我按下Shift + q,它将显示为大写'Q'。我对这种行为感到非常惊讶。 – JPReddy 2011-01-24 15:48:40

+0

它不只是'q'键。如果我使用箭头键导航到单元格并按下某个键,则该键输入将被忽略。否则它工作正常..需要'q'和其他键输入。 – 2011-01-24 15:50:16

回答

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

尝试将此代码并看到行为。 (请参阅我的意见下面你的问题)

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

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