C sharp - mover filas en datagridview

 
Vista:

mover filas en datagridview

Publicado por cesar  (9 intervenciones) el 06/10/2010 00:06:42
hola tengo un data grid enlazado a datos y kiero saver como mover las filas una moverla hacia abajo y otra hacia ariba segun un uno lo desee, tienen alguna idea.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:mover filas en datagridview

Publicado por m (97 intervenciones) el 08/10/2010 05:35:52
podrias darle un orden a tu fila.
es decir a los datos agregarle un campo
de indice u orden. Asi cada vez que quieras moverlo,
actualizas el indice del dato.

saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

mover filas en datagridview

Publicado por Gualterio (1 intervención) el 23/11/2011 21:52:41
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private List<MyItem> items = new List<MyItem> {
new MyItem {Id = 0, Name = "Hello"},
new MyItem {Id = 1, Name = "World"},
new MyItem {Id = 2, Name = "Foo"},
new MyItem {Id = 3, Name = "Bar"},
new MyItem {Id = 4, Name = "Scott"},
new MyItem {Id = 5, Name = "Tiger"},
};

private BindingSource bs;

private void Form1_Load(object sender, EventArgs e)
{
bs = new BindingSource(items, string.Empty);
dataGridView.DataSource = bs;
//DataGridViewRow dr = new DataGridViewRow();

//string[] row1 = new string[] { "CAMPO001"};
//this.dataGridView.Rows.Add(row1);
//string[] row2 = new string[] { "CAMPO002" };
//this.dataGridView.Rows.Add(row2);
//string[] row3 = new string[] { "CAMPO003"};
//this.dataGridView.Rows.Add(row3);
//string[] row4 = new string[] { "CAMPO004" };
//this.dataGridView.Rows.Add(row4);
//string[] row5 = new string[] { "CAMPO005" };
//this.dataGridView.Rows.Add(row5);
//string[] row6 = new string[] { "CAMPO006" };
//this.dataGridView.Rows.Add(row6);
//this.dataGridView.RowHeadersVisible = false;
}

private void button1_Click(object sender, EventArgs e)
{
int position = bs.Position;
if (position == 0) return; // already at top

bs.RaiseListChangedEvents = false;

MyItem current = (MyItem)bs.Current;
bs.Remove(current);

position--;

bs.Insert(position, current);
bs.Position = position;

bs.RaiseListChangedEvents = true;
bs.ResetBindings(false);
}


private void button2_Click(object sender, EventArgs e)
{
int position = bs.Position;
if (position == bs.Count - 1) return; // already at bottom

bs.RaiseListChangedEvents = false;

MyItem current = (MyItem)bs.Current;
bs.Remove(current);

position++;

bs.Insert(position, current);
bs.Position = position;

bs.RaiseListChangedEvents = true;
bs.ResetBindings(false);
}
public class MyItem
{
public int Id { get; set; }
public String Name { get; set; }
}

}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar