Doubt on How to pass selected lines from one datagridview to another

Asked

Viewed 148 times

1

good evening.I am developing a Windownsform application with C# and mysql, I have some doubts about how to pass data from one datagridview to another. Datagridviews are found in different forms. I have some code already done that I can already pass the selected lines. I use a checkbox to choose the lines and send them to the datagridview via a button. Only every time I choose a new line and send it, the previously chosen codes always have True value.

this is the boot code to pass the chosen lines:

 for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)

            {
                if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[9].Value) == true)
                {
                    RecordId = RecordId + dataGridView1.Rows[i].Cells["Codigo"].Value.ToString() + ",";

                    MessageBox.Show("selected Id " + RecordId + "check" + dataGridView1.Rows[i].Cells[9].Value.ToString());
                    // RemoveDuplicate(dataGridView1);

                }

            }

This is the code that loads the datagridview to choose the lines:

 private void carregaDados()
    {
        db = new BDconexao.accessBD();
        dataGridView1.DataSource = null;
        dataGridView1.Rows.Clear();
        dataGridView1.Refresh();

        string connectionString = db.getConnectionString();
        //string query = "SELECT * FROM   protese UNION (SELECT * FROM tratamento_estomat) ";
        //string query = "SELECT id, descricao, codigo, preco_clinica, comparticipacao_perc, comparticipacao_max,comparticipacao_inps,comparticipacao_segurado, total_tratamento   FROM   protese  ";
        //query += "UNION (SELECT id, descricao, codigo, preco_clinica, comparticipacao_perc, comparticipacao_max, comparticipacao_inps,comparticipacao_segurado, total_tratamento FROM tratamento_estomat)";
        string query = "SELECT * FROM ((SELECT id, descricao, codigo, preco_clinica, comparticipacao_perc, comparticipacao_max,comparticipacao_inps,comparticipacao_segurado, total_tratamento FROM protese)";
        query += "UNION (SELECT id, descricao, codigo, preco_clinica, comparticipacao_perc, comparticipacao_max, comparticipacao_inps, comparticipacao_segurado, total_tratamento FROM tratamento_estomat) )";
        query += "AS tb ";

        using (MySqlConnection conn = new MySqlConnection(connectionString))
        {
            using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn))
            {
                try
        {
                    //******
                    DataTable dataTable = new DataTable();



                    DataGridViewCheckBoxColumn dgvcCheckBox = new DataGridViewCheckBoxColumn();
                    dgvcCheckBox.HeaderText = "Marcar";

                    dgvcCheckBox.Name = "Marcar";
                    dgvcCheckBox.ReadOnly = true;
                    //dgvcCheckBox.FalseValue = "False";
                    //dgvcCheckBox.TrueValue = "True";
                    dataGridView1.Columns.Insert(9, dgvcCheckBox);

                    //******
                    adapter.Fill(dataTable);

                    for (int i = 0; i < dataTable.Rows.Count; i++)
                    {
                        dataGridView1.Rows.Add(dataTable.Rows[i][0], dataTable.Rows[i][2], dataTable.Rows[i][1], dataTable.Rows[i][3], dataTable.Rows[i][4], dataTable.Rows[i][5], dataTable.Rows[i][6], dataTable.Rows[i][7], dataTable.Rows[i][8]);
                    }
                    dataGridView1.AllowUserToAddRows = false;
                    dataGridView1.AllowUserToDeleteRows = false;

                }

                catch (Exception ex)
                {
                    MessageBox.Show("Error" + ex);
                }

           }
        }
    }

following images to understand the idea:

datagridview para escolha das linhas

essa imagem mostra a repetição das linhas escolhidas anteriormente

I’d really appreciate your help getting through this. Thank you all

  • A suggestion of mine would be to create a separate method that receives the codes of the selected records as argument and based on these codes I would run a search in the database and the result I would play in a List<MeuModelo> and return it. It would be enough to assign the return of the method to the property DataSource of DataGridView.

  • Thanks for the answer. I have tried with list but I still have the same problem. I will continue to investigate but I accept other ideas or suggestions that allow me to overcome this problem

  • Thank you very much, I did using another logic and it works very well.

No answers

Browser other questions tagged

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