4

ここに画像の説明を入力

  1. ユーザーが 1 つの行を選択する
  2. 上矢印と下矢印があります。
  3. ユーザーが上に移動したい場合、ユーザーは上矢印ボタンをクリックします
  4. ユーザーが下に移動したい場合、ユーザーは下矢印ボタンをクリックします
  5. 行が一番上にある場合、上矢印ボタンは無効になります
  6. 行が一番下にある場合、下矢印ボタンは無効になります

私はこのコードを試しましたが、上記のシナリオではまったく機能しませんでした

private void key_up(オブジェクト送信者, 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;
    }
}
4

7 に答える 7

1

私にとってこれはうまくいきました:

 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 - 2) の代わりに (rowCount - 1) を使用したことです...これを変更した後、完全に機能しました。DataGridView に 2 行しかない場合、下に移動する前に機能しませんでした...

于 2016-09-01T17:52:26.923 に答える
1

その問題に対する非常に小さな解決策は次のとおりです。

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

    }

申し訳ありませんが、私のコードは一目瞭然だと思いました。コメントで、その問題をどのように進めたかを明確にしたことを願っています

于 2017-09-01T09:53:23.553 に答える
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;
 }
于 2014-05-06T13:19:40.477 に答える
0
    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();
        }        
于 2020-06-27T06:16:39.263 に答える