2010-02-11 23 views
2

我正在使用Infragistics UltraWinGrid v9.1。 我想允許用戶在單元格中輸入數字數據,請按輸入,然後將焦點放在下面的單元格上,就像您在Excel中看到的一樣。看起來KeyUp事件可能比KeyPressed事件更好,但我一直拋出一個異常,即使我從完整網格的頂部開始,我已經超出了UltraWinGrid的界限。這裏是我試過的代碼:如何按下Enter鍵導致焦點移動到下面的單元格,如Excel的默認行爲?

private void ugrid_KeyUp(object sender, KeyEventArgs e) 
    { 
     UltraGrid grid = (UltraGrid)sender; 

     if (e.KeyCode == Keys.Enter) 
     { 
      // Go down one row 
      UltraGridCell cell = grid.ActiveCell; 
      int currentRow = grid.ActiveRow.Index; 
      int col = cell.Column.Index; 
      grid.Rows[currentRow + 1].Cells[grid.ActiveCell].Activate(); 
     } 
    } 

我預計這使細胞在同一列,但低於一排變成了與調用活動單元格, grid.Rows [currentRow + 1]。細胞[grid.ActiveCell] .Activate();

相反則拋出異常:

類型的異常 「System.IndexOutOfRangeException」 發生在 Infragistics2.Shared.v9.1.dll但 不是在用戶代碼來處理附加信息 :索引超出了陣列的 界限。

由於我在第0行,並且存在第1行,這對我來說是一個驚喜。 currentRow和col的值分別爲0和28。什麼是更好的方法? btw我可以再次做到這一點的單元格,其中的值是currentRow = 1和col = 28。拋出同樣的異常。

回答

6

有人回答我有關的Infragistics論壇的問題...

private void ugrid_KeyUp(object sender, KeyEventArgs e) 
    { 
     var grid = (UltraGrid)sender; 

     if (e.KeyCode == Keys.Enter) 
     { 
      // Go down one row 
      grid.PerformAction(UltraGridAction.BelowCell); 
     } 
    } 
2

,如果我說的是也爲V9.1有效,但你也可以做這樣的事情,我不知道:

yourGrid.KeyActionMappings.Add(new GridKeyActionMapping(Keys.Enter, UltraGridAction.BelowCell, 0, UltraGridState.Row, SpecialKeys.All, 0)); 
1
private void ulGrvProducts_KeyUp(object sender, KeyEventArgs e) 
     { 

      UltraGrid grid = (UltraGrid)sender; 

      if (e.KeyCode == Keys.Enter) 
      { 
       //Go down one row 
       grid.PerformAction(UltraGridAction.BelowCell); 
      } 
     } 
0

使用grid.PerformAction(UltraGridAction.BelowCell)後,活動行會改變,但下一個單元格不是在編輯模式。

+0

如果您使用此「grid.PerformAction(UltraGridAction.EnterEditMode);」在PerformAction之後,單元格將處於編輯模式。 –

相關問題