2 Datagridview when clicking the first play the record in 2?

Asked

Viewed 121 times

1

I’m using Winforms, I already have one Gridview populated, now I need that when the user double-click on a given line, he removes this record from the first grid and plays the same record on the second grid.

If possible, could you do this with more than 1 record at a time? Example I press Shift and I click on the record right after I click on the button and it sends to the second Grid.

  • Po, but if you send as soon as you first click, how will you have reference to select multiples? The best would be to use a keyboard key to send as shortcut, and a... send button automatically on the first click becomes strange not?

  • To send multiple would hold Shift, I think I’ll leave with 2 clicks, so no error. Thanks for the remark Miguel

  • @William as you are doing the Bind of the grid?

  • @jbueno, I’m using dgvGrupos.Datasource = bs_groups; Using the foreach of the list that Miguel passed, I was able to migrate the records from one grid to the other. I just need to make sure you don’t have a performance problem.

2 answers

4


For the button to move the records you can do so:

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 amount of selected records before entering for, because Datagridview always selects the next element when one is eliminated... otherwise, what happens is that it ends up excluding everything from there down.

For double click 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 beforehand the data-Binding of each of the Datagridview. I created a Bindingsource for each grid.

EDIT

How to configure Bindingsource:

  1. Abra as ações do DataViewGrid no designer

  2. Selecione a opção 'Object'

  3. Selecione o tipo do objeto desejado

To fill in Bindingsource simply iterate the list elements and add them to 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 };
  • Miguel, in case I have a class and used it as a list. public string Name{ get; set; } public string Url { get; set; } ------ list.Add(groups);

  • Could you reuse it to do that? Just look: List<Groups> list = new List<Groups>(); List<Groups> selected list = new List<Groups>();

  • You can copy the list items to Bindingsource... but using the list directly has no way. I will update reply.

  • You really need to use a foreach to get the list items into Bindingsource?

  • 1

    I thought so too... but I couldn’t find a method AddRange in it. So I think so. If this is a nuisance, it’s always possible to implement an Extension-method to cover that possibility.

  • Setting the list to Datasource does not work?

  • It may even work to read items, but it does not work to warn Datagridview that the list has changed. The class List<T> does not have events to warn Datagridview that something has been added to it... so I used Bindingsource, because when something is added to it, it warns the controller that the list has been updated.

  • @Miguel worked here! Thanks a lot for the help. About the foreach I ended up using like this: bs_groups = new Bindingsource() { Datasource = list };

  • Good thing... I’m teaching but I’m also learning! That last one I didn’t know.

  • I had to convert to the list using the following code as well: list = Bindingsource.Cast<Groups>(). Tolist(); If possible change your answer to these 2 pq codes can help other users. Again thanks for the proposed solution!

Show 5 more comments

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

        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.