Send data Drag and Drop from one Grid to another

0

In my project I had the following question:

  • I have 2 dataGridView , where dataGridView1 would be a list of products (one query SQL of products).
  • I would like to know if I can drag this dataGridView1 and drop the dataGridView2 that would be the purchase that the customer is taking and be able to save this other information as invoice #xxx.

I'm trying with this code and the item is not sent, what will it be?

   using Npgsql;
         namespace WindowsFormsApplication1
{
public partial class Form2 : Form
    {
    public event DragEventHandler DragDrop;
    private DataSet ds = new DataSet();
    private DataTable dt = new DataTable();
    public Form2()
    {
        InitializeComponent();
    }
private void button1_Click(object sender, EventArgs e)
    {
        try
        {



            string connstring = String.Format("Server=localhost;Port=5432;User Id=postgres;Password=123;Database=BDsystem");

            NpgsqlConnection conn = new NpgsqlConnection(connstring);
            conn.Open();

            string sql = "SELECT * FROM simple_table";

            NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);

            ds.Reset();

            da.Fill(ds);

            dt = ds.Tables[0];

            dataGridView1.DataSource = dt;

            conn.Close();
        }
        catch (Exception msg)
        {

            MessageBox.Show(msg.ToString());
            throw;

        }
    }
    private void MouseDown(object sender, MouseEventArgs e)
    {
        var hitTestInfo = dataGridView1.HitTest(e.X, e.Y);
        if (hitTestInfo.RowIndex > -1)
        {
            dataGridView1.DoDragDrop(hitTestInfo.RowIndex,
                DragDropEffects.Copy | DragDropEffects.Move);
        }

    }
    private void dataGridView2_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = e.Data.GetDataPresent(DataFormats.StringFormat) ? DragDropEffects.Copy : DragDropEffects.None;
    }
    private void dataGridView2_DragDrop(object sender, DragEventArgs e)
    {
        string dropData = e.Data.GetData(DataFormats.Text).ToString();
        int rowIndex = int.Parse(dropData);

        var cells = dataGridView1.Rows[rowIndex].Cells.Cast<DataGridViewCell>()
                        .Select(c => c.Value).ToArray();
        dataGridView2.Rows.Add(cells);

        dataGridView1.Rows.RemoveAt(rowIndex);
    }
   }
}
    
asked by Gilberto Asuaje 11.10.2016 в 05:57
source

1 answer

3

In addition to defining the property AllowDrop = true you should use some event to execute the movement of the rows between grids

Events are DragEnter and DragDrop

private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
    var hitTestInfo = dataGridView1.HitTest(e.X, e.Y);
    if (hitTestInfo.RowIndex > -1)
    {
        dataGridView1.DoDragDrop(hitTestInfo.RowIndex,
            DragDropEffects.Copy | DragDropEffects.Move);
    }
}

private void dataGridView2_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = e.Data.GetDataPresent(DataFormats.StringFormat) ? DragDropEffects.Copy : DragDropEffects.None;
}

private void dataGridView2_DragDrop(object sender, DragEventArgs e)
{
    string dropData = e.Data.GetData(DataFormats.Text).ToString();
    int rowIndex = int.Parse(dropData);

    var cells = dataGridView1.Rows[rowIndex].Cells.Cast<DataGridViewCell>()
                    .Select(c => c.Value).ToArray();
    dataGridView2.Rows.Add(cells);

    dataGridView1.Rows.RemoveAt(rowIndex);
}

The idea is to pass the index of the row from one grid to the other when the drag drop event occurs

Select and drag to datagridview row with a single click

Drag row from a datagridview to another datagridview

    
answered by 11.10.2016 / 13:32
source