2013-07-02 1 views

답변

0

당신은 dataGridView1_KeyDown 이벤트를 사용해야합니다 :

private void dataGridView1_KeyDown(object sender, KeyEventArgs e) 
     { 
      if (e.KeyCode == Keys.Enter) 
      {     
       e.SuppressKeyPress=true; 
       //block of code 
      } 
     } 
+0

조건 – user2541238

+0

을 실행할 위치가 datagridviewcell 편집 모드에서도 작동합니다. –

+0

인 경우 코드 블록 – user2541238

1

에서 KeyDown이 편집 모드, 서브 클래스 DataGridView에있는 셀에 대한 작업과 같은 ProcessDialogKey를 대체하지 않습니다.

protected override bool ProcessDialogKey(Keys keyData) 
{ 
    if (keyData == Keys.Enter) 
    { 
     // Your code here 
     return true; 
    } 
    return base.ProcessDialogKey(keyData); 
} 
+0

우리가 원하는 것은, 셀 1의 엔터 키를 누를 때 코드의 block1을 실행하고 셀 2의 엔터 키를 누르면 코드 블록 2를 실행하는 식입니다. 어떤 block1의 코드는리스트 뷰를 포함하는 모달 박스를 보여줄 것입니다. – user2541238

+0

입력 키를 가로 채고 코드를 추가하여 내가 지정한 대화 상자를 표시하는 방법을 보여 줬습니다. – Gary

+0

@ user2541238'CurrentCell' 속성을 참조하여 코드를 적절하게 실행하기 위해 편집 모드에있는 셀을 확인해야합니다. –

0

클릭하면 어떤 셀이 클릭되었는지 확인할 수 있습니다. 이것은 VB.net에 있습니다. 게리가 이미 제안한 것을 확장했습니다.

공공 클래스 Custom_DataGridView 상속 System.Windows.Forms.DataGridView

공공 이벤트 DataGridView_CustomEnterKeyPressed (DataGridViewCell으로 ByVal의 keyData 키로서, ByVal의 CurrentCell)

보호 오버라이드 (override) 기능 ProcessDialogKey (ByVal의 keyData 키로) 부울로 만약 keyData = Keys.Enter Then RaiseEvent DataGridView_CustomEnterKeyPressed (keyData, CType (Me, DataGridView) .CurrentCell) 'Return true'다음 셀로 이동하고 싶지 않다면 이것을 연다. n로 이동 열에 ext 셀. 최종면 반환 MyBase.ProcessDialogKey (keyData) 최종 기능

최종 클래스

사용하는 대신 상자 DataGridView에 통제와 양식에 아래 그림과 같이 다음은 DataGridView_CustomEnterKeyPressed 이벤트를 처리해야이 Custom_DataGridView 이 사용자 정의 컨트롤을 추가했습니다.

개인 서브 DataGridNameYouHaveUsed_DataGridView_CustomEnterKeyPressed은 (ByVal의 keyData는 System.Windows.Forms.Keys으로 DataGridViewCell으로 ByVal의 CurrentCell)는 "(셀 DataGridView_CustomEnterKeyPressed 이벤트 처리기에서"DataGridNameYouHaveUsed.DataGridView_CustomEnterKeyPressed 있는 MsgBox를 (핸들 + CurrentCell.ColumnIndex.ToString + ", "+ CurrentCell.RowIndex.ToString +") ") End Sub

다른 사람들 (나 같은)이 솔루션을 검색 할 때 유용 할 수도 있으므로 이미 게시 한 것일 수도 있습니다.

관련 문제