How to pass lines from one Datagridview to another?

Asked

Viewed 7,874 times

0

I wish to pass the selected lines of a datagrid for another, for example, I select lines, click on a button, and they pass to one another. Is to make an add - remove items.

I have two datagrids, one will receive Products, and the other will be to add the products you want to reference. The button will be to add these selected items into the other grid. I’m filling that first grid with a DataAdapter/DataTable.

Follow: DG1 | Button | DG2

items - Click-> .. . items

-Get out of here --> Get in here.

2 answers

5

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:

inserir a descrição da imagem aqui

  • 1

    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.

  • 1

    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.

  • 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.

0

There are several ways to do this, depending on the objects the datagrids use.

As you give no further details, assuming you have 2 equal datagrids:

  • use the same object list for the 2 datagrids
  • add a property to this object that defines whether it belongs to 'a grid 1 or 2 (with only 2 datagrids, type Boolean will suffice)
  • use this property to define line visibility in both datagrids
  • the button reverses only the property

Browser other questions tagged

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