How to pass rows from one DataGridView to another?

0

I want to pass the selected rows from one datagrid to another, for example, I select the rows, I click on a button , and they switch to another. Is to make a 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 to the other grid . I am filling this first grid with a DataAdapter / DataTable .

Follows: DG1 | Button | DG2

items - Click -> ..             .. items

-Get out of here - > Here.

    
asked by anonymous 29.01.2014 / 23:03

2 answers

5

So, you can try this way:

Assuming your products have a unique ID, I suggest using a column of type CheckBox in the DataGridView. Use the DataGrid "Cell Content Click" event to verify that the column you clicked was the check box (e.ColumnIndex == 0), where 'e' is the DataGridViewCellEventArgs of the event. If it is, assign the value 1 to it (dataGridViewMovimentos.CurrentRow.Cells [0] .Value = 1;).

(Somethingsthere,likethevalue,areusedinmyprogram,soyoucanignoreit.)Theimageisforreferenceonly,theimportantpartisifsandthecellvaluechange.

Next,addtothebuttonclickeventtheroutinebelowtocheckallrows,andwhenyoufindacheckBoxwithavalueof1,copythatrowtotheothercolumn(IsuggestusingtheproductIDinthelastpositionofthetable,withthepropertyvisible=false).

(You have to make the newRow.Cells [position] .value for each cell you want to transfer and remove it for those you want to remove. Remember that your target dataGrid must have exactly the same columns as the row you are returning Then remove it where you need it.) Try to do it that way there. I tested it here and it worked.

I ask someone to format this text. I'm new here, so I could not format it.

Other questions, contact me and I'll try to help you. I'm at work now, but I'll answer you as soon as I can. Q: This is my screen with the CheckBox at the beginning:

    
30.01.2014 / 13:31
0

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

As you do not give more details, assuming you have 2 equal datagrids:

  • use the same list of objects for the 2 datagrids
  • add a property to this object that defines whether it belongs to grid 1 or 2 (with only 2 datagrids, boolean type will suffice)
  • Use this property to set line visibility in both datagrids
  • the button only reverses the property
29.01.2014 / 23:17