So you can try it this way:
Assuming your products have a unique ID, I suggest using a column like CheckBox
in the DataGridView
. Use the event from DataGrid "Cell Content Click"
to verify that the clicked column was CheckBox
(e.ColumnIndex == 0)
, being 'and' the DataGridViewCellEventArgs
of the event.
If it is,
assign value 1 to it (dataGridViewMovimentos.CurrentRow.Cells[0].Value = 1;
).
private void dataGridViewMovimentos_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if(e.columnIndex == 0)
{
if(Convert.ToInt32(dataGridViewMovimentos.CurrentRow.Cells[0].Value) == 0)
{
valorTransferencia += Convert.ToDouble(dataGridViewMovimentos.CurrentRow.Cells[5].Value);
txtBoxTotalTransferencia.Text = valorTransferencia.ToString();
dataGridViewMovimentos.CurrentRow.Cells[0].Value = 1;
}
else
{
valorTransferencia += Convert.ToDouble(dataGridViewMovimentos.CurrentRow.Cells[5].Value);
txtBoxTotalTransferencia.Text = valorTransferencia.ToString();
dataGridViewMovimentos.CurrentRow.Cells[0].Value = 0;
}
}
}
(Some things there, like the value, are used in my program. So you can ignore it. The image is just for reference. The important part is the Ifs and the Value change of the cell).
Then add to the event click the below routine button to check all lines and when you find a CheckBox
with value 1, copy that row to the other column(I suggest using the product ID at the last table position, with the property visible = false
).
private void button1_Click(object sender, EventArgs e)
{
foreach(DataGridViewRow row in dataGridView1.Rows)
{
dataGridView2.Rows.Add(adicionar(row));
}
}
private DataGridViewRow adicionar(DataGridViewRow row)
{
DataGridViewRow newRow = (DataGridViewRow)row.Clone();
newRow.Cells[1].Valeu = row.Cells[1].Value;
newRow.Cells[2].Valeu = row.Cells[2].Value;
newRow.Cells.Remove(newRow.Cells[0]);
return newRow;
}
(You have to do the newRow.Cells[posicao].value
for each cell you want to transfer and remove it to the ones you want to remove.
Remember that your DataGrid
destination has to have exactly the same columns as the row you are returning. Then do the Remove where you need it.
Try to do it that way there. I tested it here and it worked.
P.S.: That’s my screen with the CheckBox
at first:
Prefer to post the code here instead of the screenshot of it. The screenshot the result is cool. When you keep code with 4 spaces on all lines, it formats and makes Highlight.
– Maniero
Beauty @bigow. I started yesterday, and I haven’t had time to study how site formatting works. I was posting the code, but it wasn’t getting formatted. Then, to improve the view, I put the screenshot.
– 00lenon
Use four spaces at the beginning of each line or a TAB before copying it all out. You can put <!-- language: c# -> a line before the code, separated by a blank line, to make the colors in the code.
– Alexandre Marcondes