Good evening. I'm developing a Windownsform application with C # and mySql , I'm having some questions on 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 pick the rows and send them to the datagridview via a button. But whenever I choose a new line and send it, the previously chosen codes are always True.
This is the button code for passing the chosen rows:
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:
I would greatly appreciate your help in overcoming this. Thank you all