2011-02-26 5 views
1

나는 datagridview 필터링을위한 애플리케이션을 개발했다. 그리고 나는 필터링을 위해 datagridview의 dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) 이벤트를 사용했습니다. 하지만 datagridview 셀의 키 누르기 이벤트에서 처리하려고합니다. 그러나 나는 그런 종류의 사건을 얻지 못하고있다.DataGridview 컨트롤의 이벤트에 대해서

각 키를 누를 때 을 발생 시킬수해야 DataGridView에 이벤트 ..

그래서 아무도 나에게 내가 DataGridView를 사용해야하는 이벤트 것을 알 수 있습니까?

좀 도와주세요 ... 고맙습니다

답변

3

DataGridView.KeyPress 이벤트가 가 발생되지 않습니다 때 특정 셀에 사용자 유형.

  1. 은 사용 액세스 할 수있는 편집 컨트롤 자체 (직접 제기하는 KeyPress 이벤트를 처리 : 당신은 셀의 내용을 편집하는 동안 그들은 키를 누를 때마다 통지 할 경우 두 가지 옵션이 있습니다 EditingControlShowing 이벤트).

    는 예를 들어, 다음 코드를 사용할 수 있습니다

    public class Form1 : Form 
    { 
        public Form1() 
        { 
         // Add a handler for the EditingControlShowing event 
         myDGV.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(myDGV_EditingControlShowing); 
        } 
    
        private void myDGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
        { 
         // Ensure that the editing control is a TextBox 
         TextBox txt = e.Control as TextBox; 
         if (txt != null) 
         { 
          // Remove an existing event handler, if present, to avoid adding 
          // multiple handler when the editing control is reused 
          txt.KeyPress -= new KeyPressEventHandler(txt_KeyPress); 
    
          // Add a handler for the TextBox's KeyPress event 
          txt.KeyPress += new KeyPressEventHandler(txt_KeyPress); 
         } 
        } 
    
        private void txt_KeyPress(object sender, KeyPressEventArgs e) 
        { 
         // Write your validation code here 
         // ... 
    
         MessageBox.Show(e.KeyChar.ToString()); 
    
        } 
    } 
    
  2. 표준 DataGridView 컨트롤에서 상속 사용자 정의 클래스를 작성하고 ProcessDialogKey method 우선합니다. 이 메서드는 편집 컨트롤에서 발생하는
    조차도 각 키 이벤트를 처리하도록 디자인되었습니다. 오버라이드 된 메소드 내에서 키 누르기를 처리하거나 별도의 핸들러 메소드를 첨부 할 수있는 자체 이벤트를 발생시킬 수 있습니다.

+0

그러나 rowindex 및 열 인덱스도 원합니다. 그렇다면 어떻게 위의 코드에서 rowindex와 column 인덱스를 얻을 수 있습니까? – priyanka

+0

@priyanka : 가장 간단한 방법은 ['DataGridView.CurrentCell' 속성] (http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcell.aspx)을 사용하는 것입니다. 현재 선택되어있는 [cell] (http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.aspx)을 반환 할 것이고, 차례로 ['ColumnIndex'] (http : //msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.columnindex.aspx) 및 ['RowIndex'] (http://msdn.microsoft.com/en-us/library/ system.windows.forms.datagridviewcell.rowindex.aspx) 속성을 사용합니다. –

+0

DataGridview 현재 셀에 e.keychar 값을 설정하는 방법은 무엇입니까? 왜냐하면 내가 내 응용 프로그램에서 위의 이벤트를 사용할 때 null 값으로 현재 셀을 보여주고 그 시간 때문에 다른 작업을 수행 할 수 없기 때문입니다. – priyanka