2014-12-26 3 views
2

6 열을 가진 datagridview가 있습니다. 열의 마지막 셀에서 "Tab"버튼 만 누를 때마다 새로운 행을 추가하고 싶습니다. 추가 행마다 않도록 전 셀 값을 쓸새로운 행 추가를위한 datagridview keypress 이벤트

dataGridView1.AllowUserToAddRows = false; 
dataGridView1.Rows.Add(); 

내가 이미 [5] (마지막 셀)하지만, 마지막 셀에만

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (dataGridView1.CurrentCell.ColumnIndex == 5) 
    { 
     if (e.KeyChar == (char)Keys.Tab) 
     { 
      dataGridView1.Rows.Add(); 
     } 
    } 
} 
읽기로 설정되지 않은 셀의 키 이벤트를 사용 가질

시간 내 주셔서 감사합니다. 제 영어에 대해 미안 해요

+1

가 그것을 chancing보십시오 : dataGridView1.Rows.Add (새에 DataGridViewRow()); – Sefa

+0

"마지막 셀이 읽기 전용으로 설정되어 작동하지 않습니다"라는 의미는 무엇입니까? – Dan

+0

내 편집 된 답변보기 – TaW

답변

1

현재 셀이 DGV의 마지막 셀이고 사용자가 Tab을 누르면 현재 셀이 마지막 셀일 경우에만 이 추가됩니다.

(첫 번째 셀을 통해 backtabbing에 의한 경우를 제외하고 DGV 아웃 할 수 없습니다 탭, 이제 (명백하게) 사용자주의 ..)

int yourLastColumnIndex = dataGridView.Columns.Count - 1; 

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
{ 
    if (dataGridView.Focused && keyData == Keys.Tab) && 
     if (dataGridView.CurrentCell.ColumnIndex == yourLastColumnIndex 
      dataGridView.CurrentRow.Index == dataGridView.RowCount - 1) 
     { 
      dataGridView.Rows.Add(); 
      // we could return true; here to suppress the key 
      // but we really want to move on into the new row..! 
     } 

    return base.ProcessCmdKey(ref msg, keyData); 
} 

키 이벤트 중 하나를 사용하려는 시도를 DGV 결국 을 추가하는 대신 DGV을 떠나의 ..

+0

대단히 감사합니다. – Barakuan