简体   繁体   English

如何上下移动GridView多行?

[英]How to move GridView multiple rows up and down?

I have a devexpress gridview bound to a datatable, right now I move one row up and down, by following some topic post on this blog. 我有一个绑定到数据表的devexpress网格视图,现在我通过关注此博客上的一些主题帖子上下移动一行。 What I need is to move multiple rows up and down. 我需要的是上下移动多行。 for example on the button click event I have this 例如在按钮单击事件上我有这个

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

Thanks!!! 谢谢!!!

I don't know how you actually move your row since you didn't post this solution, but maybe you just looking for following options to set 由于您未发布此解决方案,因此我不知道如何实际移动行,但也许您只是在寻找以下选项来设置

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

and your code will work with multiple rows just like for one row. 并且您的代码将与多行一起工作,就像一行一样。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM