【问题标题】:How to move gridview selected row up/down on KeyUp or Keydown press如何在KeyUp或Keydown按下时向上/向下移动gridview选定的行
【发布时间】:2013-04-20 10:06:57
【问题描述】:

  1. 用户选择一行
  2. 会有向上箭头和向下箭头。
  3. 如果用户想向上移动,用户点击向上箭头按钮
  4. 如果用户想向下移动,那么用户点击向下箭头按钮
  5. 如果该行位于顶部,则向上箭头按钮将被禁用
  6. 如果该行位于底部,则向下箭头按钮将被禁用

我尝试了这段代码,但对上述情况根本不起作用

private void key_up(object sender, EventArgs 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;
    }
}

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    这样做的方法是, 在 key_up 或按钮上单击 1) 获取当前选中的行索引 2) 只要​​索引+1小于行数,就设置当前选中的行为(index+1)。

    对 key_Down 或按下的按钮进行否定。

        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;
                }
            }
        }
    

    你可以看到我已经将向上和向下移动的方法分开了,所以如果你想使用按钮点击事件而不是向上和向下键事件,你可以在需要时调用它们。

    【讨论】:

    • 实际上按照我的要求....如上图所示..假设如果我选择突出显示的行并按下向上键,该行应该向上移动,而它上面的行应该向下。 ..你能帮我解决这个问题吗……非常紧急……
    • 查看我编辑的代码,你必须使用 Key_Down 事件而不是 Key_Up 事件。如果您将数据源绑定到此网格视图,您也不能使用它。
    • thanx Jegan .....根据我的要求,我稍微修改了一下,它对我有用....
    【解决方案2】:

    如果您想根据需要向上/向下移动所选行,可以使用以下代码移动:

    向上:

    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;
     }
    

    【讨论】:

      【解决方案3】:

      清理了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;
          }
      

      【讨论】:

      • 太棒了。工作一种享受。
      【解决方案4】:

      对我来说这很有效:

       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 行,则在向下移动之前不起作用...

      【讨论】:

        【解决方案5】:

        以下是针对该问题的一个非常小的解决方案:

            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");
                }
        
            }
        

        抱歉,我认为我的代码是不言自明的。 我希望我与 cmets 清楚我是如何处理该问题的

        【讨论】:

        • 想用 cmets 或结论解释一下你的代码在这里做什么?
        • 你可以吗?
        【解决方案6】:

        简历:

        private void MoveUpDown(bool goUp)
            {
                int newRowIndex = DataGridView.SelectedCells[0].OwningRow.Index + (goUp ? -1 : 1);
                if (newRowIndex > -1 && newRowIndex < DataGridView.Rows.Count)
                {
                    DataGridView.ClearSelection();
                    DataGridView.Rows[newRowIndex].Selected = true;
                }
            }
        

        【讨论】:

        • 请用英文发表您的问题和答案。这是一个被许多不同国籍的人使用的网站。
        【解决方案7】:
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//FullRowSelect
             int selectedrowindexindgv2 =-1;
              private void moveUp()
                    {
                        if (dataGridView2.RowCount <= 0)
                            return;
            
                        if (dataGridView2.SelectedRows.Count <= 0)
                            return;
            
                        if (selectedrowindexindgv2 <= 0)
                            return;
            
                        DataGridViewRowCollection rows = dataGridView2.Rows;
            
                        // remove the previous row and add it behind the selected row.
                        DataGridViewRow prevRow = rows[selectedrowindexindgv2 - 1];
                        rows.Remove(prevRow);
                        prevRow.Frozen = false;
                        rows.Insert(selectedrowindexindgv2, prevRow);
                        dataGridView2.ClearSelection();
                        dataGridView2.Rows[selectedrowindexindgv2 - 1].Selected = true;
                        selectedrowindexindgv2 -= 1;
                        return;
                    }
            
                    private void moveDown()
                    {
                        try
                        {
                            if (dataGridView2.RowCount <= 0)
                                return;
            
                            if (dataGridView2.SelectedRows.Count <= 0)
                                return;             
            
                            if (selectedrowindexindgv2 == dataGridView2.Rows.Count - 1) // Here used 1 instead of 2
                                return;
            
                            DataGridViewRowCollection rows = dataGridView2.Rows;
                            // remove the next row and add it in front of the selected row.
                            DataGridViewRow nextRow = rows[selectedrowindexindgv2 + 1];
                            rows.Remove(nextRow);
                            nextRow.Frozen = false;
                            rows.Insert(selectedrowindexindgv2, nextRow);
                            dataGridView2.ClearSelection();
                            dataGridView2.Rows[selectedrowindexindgv2 + 1].Selected = true;
                            selectedrowindexindgv2 += 1;
                        }catch(Exception) { }
            
                    }
            
               private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
                    {
                        if (dataGridView2.Rows.Count > 0 && e.RowIndex >= 0 && e.ColumnIndex >= 0)
                        {
                            try
                            {
                                selectedrowindexindgv2 = e.RowIndex;
            }
          private void pictureBox6Up_Click(object sender, EventArgs e)
                {
                    moveUp();
                }
        
                private void pictureBox7Down_Click(object sender, EventArgs e)
                {
                    moveDown();
                }        
        

        【讨论】:

        • 欢迎来到 SO!请评论您的答案,以便在不阅读代码侦听的情况下澄清您的方法与其他方法有何不同?
        猜你喜欢
        • 2023-03-18
        • 2010-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多