2013-02-01 44 views
1

我正在開發一個windows form,其中包含兩個控件TextboxDatagridview向下和向上箭頭鍵不滾動DataGridView中的當前單元格

現在,當我第一次打開窗體焦點應該在文本框,如果我輸入的東西它應該被附加到文本框。

如果我按UP/Down arrow key重點應在DataGridviewHighlight應該移動到下一行。

但問題是它只將焦點設置爲gridview但不選擇下一行。它滾動down/UP on next key press。如果注意力集中在GridViewif I start typing alphabets it should be appended to TextBox text上,它也是一樣,但這裏它也只將焦點設置爲textbox,而在另一按鍵上它開始在其中輸入文本。

如何實現這個?我現在用

代碼是:

if (e.KeyCode== Keys.Down) 
      { 
       this.DgAppDetails.Focus(); 
       int index = DgAppDetails.CurrentCell.RowIndex; 
       if (index != DgAppDetails.Rows.Count - 1) 
       { 
        if (i == 0) 
        { 
         DgAppDetails.CurrentCell = DgAppDetails.Rows[index+1].Cells[0]; 
         i++; 
        } 
       } 
      } 

      else if(e.KeyCode==Keys.Up) 
      { 
       this.DgAppDetails.Focus(); 
       int index = DgAppDetails.CurrentCell.RowIndex; 
       if (index != 0) 
       { 
        if (i == 0) 
        { 
         DgAppDetails.CurrentCell = DgAppDetails.Rows[index - 1].Cells[0]; 
         i++; 
        } 
       } 

      } 
+0

你有代碼的例子..?你也設置像'control.Focus()?[MSDN Control.Focus()方法](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus .aspx)你在DataGridView中輸入什麼事件..? – MethodMan

回答

0

你面臨的問題,是因爲你改變了CurrentCell然而,SelectedCells

試試這個:

DgAppDetails.SelectedCells.Clear(); 
DgAppDetails.SelectedCells.Insert(DgAppDetails.CurrentCell); 
相關問題