2 DataGridView when clicking on the first play record in 2?

1

I'm using WinForms , I already have a Gridview populated, now I need to double-click on a particular line, this record of the first grid and throw the same record on the second grid.

If it were possible could you do this with more than 1 record at a time? Example I tighten the Shift and I will click on the register right after I click the button and it will send it to the second Grid.

    
asked by anonymous 21.07.2016 / 21:41

2 answers

4

For the move button you can do the following:

var selectedRowCount = dataGridView1.SelectedRows.Count;
if (selectedRowCount > 0)
    for (int i = 0; i < selectedRowCount; i++)
    {
        var obj = dataGridView1.SelectedRows[0].DataBoundItem;
        dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
        pessoaBindingSource2.Add(obj);
    }

Note that I had to store the number of records selected before entering for , because the DataGridView always selects the next element when one is deleted ... otherwise, what happens is that it ends up being excluded everything down.

For double clicking it is much easier:

if (dataGridView1.SelectedRows.Count == 1)
{
    var obj = dataGridView1.SelectedRows[0].DataBoundItem;
    dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
    pessoaBindingSource2.Add(obj);
}

It is necessary to configure the data-binding of each of the DataGridView beforehand. I created a BindingSource for each grid.

EDIT

How to configure BindingSource:

  • To populate the BindingSource, simply iterate through the elements of the list and add them to the BindingSource:

    foreach (var item in minhaLista)
        pessoaBindingSource1.Add(item);
    

    Or so, as my interlocutor taught me in one of the comments:

    new BindingSource() { DataSource = lista };
    
        
    21.07.2016 / 22:25
    0
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.RowCount > 0)
            {
                CODIGO = Convert.ToInt32(dataGridView1[0,   dataGridView1.CurrentRow.Index].Value);
                CLIENTE = dataGridView1[1, dataGridView1.CurrentRow.Index].Value.ToString();
    
       dataGridView2.Rows.Add(CODIGO,CLIENTE );
    
            }
        }
    
        
    21.07.2016 / 23:43