2009-04-10 4 views
2

내 클라이언트는 DataGridView 셀을 통해 다음 현재 셀이 기본 셀이 아닌 다른 탭으로 이동할 때이를 원합니다. 이것을 달성하는 가장 좋은 방법은 무엇입니까?DataGridView에서 탭 순서를 변경하려면 어떻게해야합니까?

+0

그리드를 쓰고 싶지 않으면 KeyUp/KeyDown 이벤트를 사용하십시오. 내 대답을 보라. 나는 그것을 시도하고 그것은 작동하는 것 ... – Ruslan

답변

2

ProcessTabKey 메서드를 재정 의하여 고유 한 DataGridView를 만듭니다. 거기 논리, SetCurrentCellAddressCore를 사용하여 다음 활성 셀을 설정하십시오. 메소드의 기본 구현은

또는 같은 선택 모드, 편집 모드, 행 상태, 경계 등 여러 가지 조건에 대한

편집을 차지하고 있음을

주, 당신의 keyup을 처리 할 수 ​​/ KeyDown 이벤트. True로 그리드의

설정 StandardTab 속성을, 그리고 다음 코드를 추가합니다 : 거기에 몇 가지 이상한 행동은 내가 많은 시간을 할애하지 않았지만,이 수행해야

private void Form1_Load(object sender, EventArgs e) 
{ 
    // TODO: Load Data 

    dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0]; 

    if (dataGridView1.CurrentCell.ReadOnly) 
     dataGridView1.CurrentCell = GetNextCell(dataGridView1.CurrentCell); 
} 

private void dataGridView1_KeyUp(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Tab) 
    { 
     dataGridView1.CurrentCell = GetNextCell(dataGridView1.CurrentCell); 
     e.Handled = true; 
    } 
} 

private DataGridViewCell GetNextCell(DataGridViewCell currentCell) 
{ 
    int i = 0; 
    DataGridViewCell nextCell = currentCell; 

    do 
    { 
     int nextCellIndex = (nextCell.ColumnIndex + 1) % dataGridView1.ColumnCount; 
     int nextRowIndex = nextCellIndex == 0 ? (nextCell.RowIndex + 1) % dataGridView1.RowCount : nextCell.RowIndex; 
     nextCell = dataGridView1.Rows[nextRowIndex].Cells[nextCellIndex]; 
     i++; 
    } while (i < dataGridView1.RowCount * dataGridView1.ColumnCount && nextCell.ReadOnly); 

    return nextCell; 
} 
0

씨 루슬란을 코드가 제대로 작동하지 않았습니다. 그의 아이디어를 가져 와서 코드를 약간 변경했습니다.

private void dataGridView1_KeyUp(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Tab) 
     { 
      if(dataGridView1.CurrentCell.ReadOnly) 
       dataGridView1.CurrentCell = GetNextCell(dataGridView1.CurrentCell); 
      e.Handled = true; 
     } 
    } 

    private DataGridViewCell GetNextCell(DataGridViewCell currentCell) 
    { 
     int i = 0; 
     DataGridViewCell nextCell = currentCell; 

     do 
     { 
      int nextCellIndex = (nextCell.ColumnIndex + 1) % dataGridView1.ColumnCount; 
      int nextRowIndex = nextCellIndex == 0 ? (nextCell.RowIndex + 1) % dataGridView1.RowCount : nextCell.RowIndex; 
      nextCell = dataGridView1.Rows[nextRowIndex].Cells[nextCellIndex]; 
      i++; 
     } while (i < dataGridView1.RowCount * dataGridView1.ColumnCount && nextCell.ReadOnly); 

     return nextCell; 
    } 
관련 문제