2017-04-10 4 views
0

私はデリゲートグリッドビューをデータテーブルにバインドしていますが、今はこのブログのトピック投稿に続いて、1行を上下に移動します。 私が必要とするのは、複数の行を上下に移動することです。 は、例えば、ボタンのクリックイベントに私はこの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; 
     } 

感謝を持っています!

答えて

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

私の質問では、私はボタンのクリックイベントのコードを置くが、私は何を下ったクリックイベントに対応しています。申し訳ありませんが、それは動作します!!!! – darielrp

0

あなたは、このソリューションを投稿していなかったので、私はあなたが実際にあなたの行を移動する方法を知りませんが、多分あなただけ

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

を設定し、あなたのコードだけで、複数の行で動作するオプションを次を探して1行のように。

+0

私はOptionsSelection.MultiSelect = trueを設定しました。移動した行は最後にクリックした行だけでした。 – darielrp

関連する問題