2012-12-27 1 views
-1

DataGridView가있는 양식이 있습니다. Datagridview에는 DataGridViewComboBoxColumn과 DataGridViewTextBoxColumn이 있습니다. 문제는 내가 사용하고자하는 것입니다 Tablight 대신에을 입력하면 셀에서 editmode에있을 때도 전환 할 수 있습니다. DataGridViewComboBoxColumn을 편집하고 EnterKey를 누른 후 다음 행으로 이동하는 것을 방지하려면 어떻게해야합니까?

screenshot of the combobox to customize

screenshot of the datagridview to customize

나는 텍스트 상자 열이 answer https://stackoverflow.com/a/9917202/249120에서 제공하는 솔루션을 구현에 succeded,하지만 난 콤보 상자 열을 위해 그것을 구현할 수 없습니다. 그것을하는 방법?

중요 : 텍스트 상자 열의 경우 defaultCellStyle은 "wrap"속성이 True 여야합니다. "CustomComboBoxColumn"을 사용하여 테스트하기로 결정 했으므로 (입력 키를 누를 때마다 탭을 누를 때와 같이 작동 함) CustomTextBoxColumn과 매우 유사한 코드를 만들려고했지만 작동하지 않습니다. :

#region CustomComboBoxColumn 

public class CustomComboBoxColumn : DataGridViewColumn 
{ 
    public CustomComboBoxColumn() : base(new CustomComboBoxCell()) { } 

    public override DataGridViewCell CellTemplate 
    { 
     get { return base.CellTemplate; } 
     set 
     { 
      if (value != null && !value.GetType().IsAssignableFrom(typeof(CustomComboBoxCell))) 
      { 
       throw new InvalidCastException("Must be a CustomComboBoxCell"); 
      } 
      base.CellTemplate = value; 
     } 
    } 
} 

public class CustomComboBoxCell : DataGridViewComboBoxCell 
{ 

    public override Type EditType 
    { 
     get { return typeof(CustomComboBoxEditingControl); } 
    } 
} 

public class CustomComboBoxEditingControl : DataGridViewComboBoxEditingControl 
{ 
    protected override void WndProc(ref Message m) 
    { 
     //we need to handle the keydown event 
     if (m.Msg == Native.WM_KEYDOWN) 
     { 
      if ((ModifierKeys & Keys.Shift) == 0)//make sure that user isn't entering new line in case of warping is set to true 
      { 
       Keys key = (Keys)m.WParam; 
       if (key == Keys.Enter) 
       { 
        if (this.EditingControlDataGridView != null) 
        { 
         if (this.EditingControlDataGridView.IsHandleCreated) 
         { 
          //sent message to parent dvg 
          Native.PostMessage(this.EditingControlDataGridView.Handle, (uint)m.Msg, m.WParam.ToInt32(), m.LParam.ToInt32()); 
          m.Result = IntPtr.Zero; 
         } 
         return; 
        } 
       } 
      } 
     } 
     base.WndProc(ref m); 
    } 
} 

#endregion 

이것의 최종 결과는 속성 데이터 소스가없는 ComboBoxColumn이며, DisplayMember는, 항목, ValueMember는하지만, Enter를 누르면하면 다음 셀로 이동합니다. 그 밖의 무엇?

+0

시도한 것에 대한 정보를 추가 할 수 있습니까? – jeremy

+0

문제가 무엇인지 잘 모르겠습니다. ?? 내가 콤보 상자의 항목을 선택하면 다음 셀로 이동한다고 생각합니까? 마우스를 사용할 때나 엔터 키만 누를 때 이런 일이 발생합니까 ?? 선택 변경 이벤트를 확인하는 것이 좋습니다. – ahmedsafan86

+0

comboboxcolumn에는 displaystyle : nothing 및 flatstyle : flat이 있습니다. 항목을 선택하고 Enter 키를 누르면 작동합니다. 수동으로 값을 쓰면 마우스를 사용하여 항목을 선택하면 다음 행으로 이동합니다 (원하는 항목입니다). 정말로) 나는 ENTER를 눌러 다음 줄로 간다. –

답변

1

답 브루노 Pacifici 응답에 코멘트 :

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
    { 
     //Override behavior on Enter press 
     if (keyData == Keys.Enter) 
     { 
      if (CurrentCell != null) 
      { 
       if (CurrentCell.IsInEditMode) 
       { 
        //Do Stuff if cell is currently being edited 
        return ProcessTabKey(keyData); 
       } 
       else 
       { 
        //Do Stuff if cell is NOT yet currently edited 
        BeginEdit(true); 
       } 
      } 
     } 
     //Process all other keys as expected 
     return base.ProcessCmdKey(ref msg, keyData); 
    } 

추신 :

또는 당신은 동일한 작업을 수행에만 1 방법 "ProcessCmdKey"를 대체 할 수 있습니다 답변이 너무 크지 않은 경우 왜 답변 만 링크를 게시 하시겠습니까? "도움이되는"링크가 더 이상 효과가 없을 때 많은 경험이있었습니다. 원본 소스에 대한 링크가있는 일부 코드를 복사하면 "404"- 안전 대답이됩니다.

관련 문제