如何上下移动GridView多行?

时间:2017-04-10 19:51:17

标签: c# gridview devexpress

我有一个绑定到数据表的devexpress gridview,现在我通过关注此博客上的一些主题帖子来上下移动一行。 我需要的是上下移动多行。 例如,按钮点击事件我有这个

private void btnMoveMotor_Up_Click(object sender, EventArgs e)
        {
            GridView view = gridView_Motores;
            view.GridControl.Focus();

            int index = view.FocusedRowHandle;
            if (index <= 0) return;

            DataRow row1 = view.GetDataRow(index);
            DataRow row2 = view.GetDataRow(index - 1);

            object idcentg = row1[Codigo];
            object idbatg = row1[batg];
            object idunig = row1[unig];
            object pot_cal = row1[potCalculada];
            object pot_trab = row1[potTrabajo];

            object idcentg1 = row2[Codigo];
            object idbatg1 = row2[batg];
            object idunig1 = row2[unig];
            object pot_cal1 = row2[potCalculada];
            object pot_trab1 = row2[potTrabajo];

            row1[Codigo] = idcentg1;
            row1[batg] = idbatg1;
            row1[unig] = idunig1;
            row1[potCalculada] = pot_cal1;
            row1[potTrabajo] = pot_trab1;

            row2[Codigo] = idcentg;
            row2[batg] = idbatg;
            row2[unig] = idunig;
            row2[potCalculada] = pot_cal;
            row2[potTrabajo] = pot_trab;

            view.FocusedRowHandle = index - 1;

            btnAplicar.Enabled = true;
            btnAplicarOrdenMotores.Enabled = true;
        }

感谢!!!

2 个答案:

答案 0 :(得分:0)

我不知道您是如何实际移动行的,因为您没有发布此解决方案,但也许您只是寻找以下选项来设置

    gridView1.OptionsSelection.MultiSelect = true;
        gridView1.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect;

并且您的代码将使用多行,就像一行一样。

答案 1 :(得分:0)

if (view.SelectedRowsCount == 1)
{
    // I do what I post in my question
     }

        if(view.SelectedRowsCount > 1)
{
      int[] pos = new int[view.SelectedRowsCount];
                    pos = view.GetSelectedRows();//save into an array the selected rows handle                    
                    int lastHandle = pos.Last();
                    DataTable tableAux = new DataTable();                               
                    tableAux.Columns.Add("idcentg", typeof(string));
                    tableAux.Columns.Add("idbatg", typeof(int));
                    tableAux.Columns.Add("idunig", typeof(int));
                    tableAux.Columns.Add("potCalculada", typeof(decimal));
                    tableAux.Columns.Add("potTrabajo", typeof(decimal));
                    tableAux.Columns.Add("orden", typeof(int));
                    tableAux.Columns.Add("capinsg", typeof(decimal));

                    for (int i = 0; i < gridView_Motores.DataRowCount; i++)
                    {
                        tableAux.Rows.Add(gridView_Motores.GetRowCellValue(i,"idcentg"), gridView_Motores.GetRowCellValue(i, "idbatg"), 
                                          gridView_Motores.GetRowCellValue(i, "idunig"), gridView_Motores.GetRowCellValue(i, "potCalculada"),
                                          gridView_Motores.GetRowCellValue(i, "potTrabajo"), gridView_Motores.GetRowCellValue(i, "orden"),
                                          gridView_Motores.GetRowCellValue(i, "capinsg"));
                    }              

                    int tag = 1;
                    int cont = 1;
                    for (int i = 0; i < pos.Length; i++)
                    {
                        if (tag == 1)
                        {
                            DataRow selectedRow = tableAux.Rows[pos[i]];
                            DataRow newRow = tableAux.NewRow();
                            newRow.ItemArray = selectedRow.ItemArray;
                            tableAux.Rows.Remove(selectedRow);
                            tableAux.Rows.InsertAt(newRow, lastHandle + 1);
                        }
                        else
                        {
                            DataRow selectedRow = tableAux.Rows[pos[i] - cont];
                            DataRow newRow = tableAux.NewRow();
                            newRow.ItemArray = selectedRow.ItemArray;
                            tableAux.Rows.Remove(selectedRow);
                            tableAux.Rows.InsertAt(newRow, lastHandle + 1);
                            cont++;
                        }
                        tag++;                                 
                    }

                    int orden = 1;
                    foreach (DataRow row in tableAux.Rows)
                    {
                        row["orden"] = orden;
                        orden++;
                    }                 

                    grid_OrdenMotores.DataSource = null;
                    grid_OrdenMotores.DataSource = tableAux;
    }