2013-04-26 46 views
3

enter image description here如何移動gridview的選擇的行上/下KEYUP或KEYDOWN按

  1. 用戶選擇一個行
  2. 會有向上箭頭和向下箭頭。
  3. 如果用戶想向上移動,用戶點擊向上箭頭按鈕
  4. 如果用戶想向下移動,然後用戶點擊向下箭頭按鈕
  5. 如果行是在隨後的向上箭頭按鈕變爲頂部殘疾人
  6. 如果行是再向下箭頭按鈕變爲禁用

我想這代碼,但不是在所有的工作對於上述方案

私人無效KEY_UP(對象發件人,即使是底部tArgs E)

{ 
    if (dataGridView1.CurrentRow == null) return; 
    if (dataGridView1.CurrentRow.Index - 1 >= 0) 
    { 

     dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentRow.Index - 1].Cells[0]; 
     dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true; 
    } 
} 

回答

14

做的方法是, 在KEY_UP或按鈕彈起點擊 1)獲取當前選擇的行的索引 2)設置當前選擇的行(索引+ 1),只要該索引+1小於行數。

對key_Down或button按下的否定做了點擊。

private void dataGridView1_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode.Equals(Keys.Up)) 
     { 
      moveUp(); 
     } 
     if (e.KeyCode.Equals(Keys.Down)) 
     { 
      moveDown(); 
     } 
     e.Handled = true; 
    } 

    private void moveUp() 
    { 
     if (dataGridView1.RowCount > 0) 
     { 
      if (dataGridView1.SelectedRows.Count > 0) 
      { 
       int rowCount = dataGridView1.Rows.Count; 
       int index = dataGridView1.SelectedCells[0].OwningRow.Index; 

       if (index == 0) 
       { 
        return; 
       } 
       DataGridViewRowCollection rows = dataGridView1.Rows; 

       // remove the previous row and add it behind the selected row. 
       DataGridViewRow prevRow = rows[index - 1]; 
       rows.Remove(prevRow); 
       prevRow.Frozen = false; 
       rows.Insert(index, prevRow); 
       dataGridView1.ClearSelection(); 
       dataGridView1.Rows[index - 1].Selected = true; 
      } 
     } 
    } 

    private void moveDown() 
    { 
     if (dataGridView1.RowCount > 0) 
     { 
      if (dataGridView1.SelectedRows.Count > 0) 
      { 
       int rowCount = dataGridView1.Rows.Count; 
       int index = dataGridView1.SelectedCells[0].OwningRow.Index; 

       if (index == (rowCount - 2)) // include the header row 
       { 
        return; 
       } 
       DataGridViewRowCollection rows = dataGridView1.Rows; 

       // remove the next row and add it in front of the selected row. 
       DataGridViewRow nextRow = rows[index + 1]; 
       rows.Remove(nextRow); 
       nextRow.Frozen = false; 
       rows.Insert(index, nextRow); 
       dataGridView1.ClearSelection(); 
       dataGridView1.Rows[index + 1].Selected = true; 
      } 
     } 
    } 

你可以看到我已經分離上下移動的方法,所以如果你要使用的按鈕點擊事件,而不是向上鍵和鍵按下事件,你可以在需要時調用它們。

+0

jegan它不適用於我的情況。 – sanjeev 2013-04-26 12:25:03

+0

什麼是不工作?你能更精確嗎? – Jegan 2013-04-26 13:34:33

+0

實際上按照我的要求.... 如上圖所示..假設如果我選擇突出顯示的行,並且按向上鍵的那一行應該向上移動,並且它的上面一行應該會下移... 請問你能否幫我解決這個問題......它非常緊急...... – sanjeev 2013-04-26 14:19:06

0

如果你想選擇的行/向下多次拉昇,只要你想,你可以使用此代碼中移動:

最多:

if (dataGridView1.SelectedRows[0].Index != 0) { 
    for (int j = 0; j < this.dataGridView1.Columns.Count; j++) { 
    object tmp = this.dataGridView1[j, dataGridView1.SelectedRows[0].Index].Value; 
    this.dataGridView1[j, dataGridView1.SelectedRows[0].Index ].Value = this.dataGridView1[j, dataGridView1.SelectedRows[0].Index - 1].Value; 
    this.dataGridView1[j, dataGridView1.SelectedRows[0].Index - 1].Value = tmp; 
    } 
    int a = dataGridView1.SelectedRows[0].Index; 
    dataGridView1.ClearSelection(); 
    this.dataGridView1.Rows[a - 1].Selected = true; 
} 

下:

if (dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count - 2) { 
    for (int j = 0; j < this.dataGridView1.Columns.Count; j++) { 
    object tmp = this.dataGridView1[j, dataGridView1.SelectedRows[0].Index].Value; 
    this.dataGridView1[j, dataGridView1.SelectedRows[0].Index].Value = this.dataGridView1[j, dataGridView1.SelectedRows[0].Index + 1].Value; 
    this.dataGridView1[j, dataGridView1.SelectedRows[0].Index + 1].Value = tmp; 
    } 
    int i = dataGridView1.SelectedRows[0].Index; 
    dataGridView1.ClearSelection(); 
    this.dataGridView1.Rows[i + 1].Selected = true; 
} 
5

清理了Jegan的代碼並使其適用於多個datagridviews。

private static void MoveUp(DataGridView dgv) 
    { 
     if (dgv.RowCount <= 0) 
      return; 

     if (dgv.SelectedRows.Count <= 0) 
      return; 

     var index = dgv.SelectedCells[0].OwningRow.Index; 

     if (index == 0) 
      return; 

     var rows = dgv.Rows; 
     var prevRow = rows[index - 1]; 
     rows.Remove(prevRow); 
     prevRow.Frozen = false; 
     rows.Insert(index, prevRow); 
     dgv.ClearSelection(); 
     dgv.Rows[index - 1].Selected = true; 
    } 

    private static void MoveDown(DataGridView dgv) 
    { 
     if (dgv.RowCount <= 0) 
      return; 

     if (dgv.SelectedRows.Count <= 0) 
      return; 

     var rowCount = dgv.Rows.Count; 
     var index = dgv.SelectedCells[0].OwningRow.Index; 

     if (index == (rowCount - 2)) // include the header row 
      return; 

     var rows = dgv.Rows; 
     var nextRow = rows[index + 1]; 
     rows.Remove(nextRow); 
     nextRow.Frozen = false; 
     rows.Insert(index, nextRow); 
     dgv.ClearSelection(); 
     dgv.Rows[index + 1].Selected = true; 
    } 
+0

太棒了。作品一種享受。 – 2017-07-06 13:17:15

0

對於我這樣的工作:

public static void MoveUp(DataGridView dgv) 
    { 
     if (dgv.RowCount <= 0) 
      return; 

     if (dgv.SelectedRows.Count <= 0) 
      return; 

     var index = dgv.SelectedCells[0].OwningRow.Index; 

     if (index == 0) 
      return; 

     var rows = dgv.Rows; 
     var prevRow = rows[index - 1]; 
     rows.Remove(prevRow); 
     prevRow.Frozen = false; 
     rows.Insert(index, prevRow); 
     dgv.ClearSelection(); 
     dgv.Rows[index - 1].Selected = true; 
    } 

    public static void MoveDown(DataGridView dgv) 
    { 
     if (dgv.RowCount <= 0) 
      return; 

     if (dgv.SelectedRows.Count <= 0) 
      return; 

     var rowCount = dgv.Rows.Count; 
     var index = dgv.SelectedCells[0].OwningRow.Index; 

     if (index == rowCount - 1) // Here used 1 instead of 2 
      return; 

     var rows = dgv.Rows; 
     var nextRow = rows[index + 1]; 
     rows.Remove(nextRow); 
     nextRow.Frozen = false; 
     rows.Insert(index, nextRow); 
     dgv.ClearSelection(); 
     dgv.Rows[index + 1].Selected = true; 
    } 

馬里奧的代碼不同的是,我用(rowCount時 - 1)替代(rowCount時 - 2)...更改此之後,它完美地工作。以前,如果你只在你的DataGridView 2行此舉向下沒有工作......

0

下面是這個問題的一個非常小的解決方案:

private void DataGridView_KeyDown(object sender, KeyEventArgs e) 
    { 
     //I use only one function for moving with the information 
     //e.KeyCode == Keys.Up = move up, else move down 
     if (e.KeyCode.Equals(Keys.Up) || e.KeyCode.Equals(Keys.Down)) 
     { 
      MoveUpDown(e.KeyCode == Keys.Up); 
     } 
     e.Handled = true; 
    } 

    private void MoveUpDown(bool goUp) 
    { 
     try 
     { 
      int currentRowindex = DataGridView.SelectedCells[0].OwningRow.Index; 

      //Here I decide to change the row with the parameter 
      //True -1 or False +1 
      int newRowIndex = currentRowindex + (goUp ? -1 : 1); 

      //Here it must be ensured that we remain within the index of the DGV 
      if (newRowIndex > -1 && newRowIndex < DataGridView.Rows.Count) 
      { 
       DataGridView.ClearSelection(); 
       DataGridView.Rows[newRowIndex].Selected = true; 
      } 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("Error"); 
     } 

    } 

對不起,我還以爲我的代碼是自-explanatory。 我希望我的意見清楚地說明了我是如何處理這個問題的

+0

小心解釋一下你的代碼在這裏做什麼,或者有評論或者結論? – 2017-09-01 10:06:48

+1

這對你有好處嗎? – 2017-09-01 12:51:20