1
votes

I have a couple of datagrids that uses a list of items as a source. I would like to move the item from list1 to list2 when the item in the grid is selected when I click a button or when I double click any of the cells in the row where the item is shown. This process will delete the selected item in the first list, and add it to the second one (it will disappear from the grid and be added to another grid which is linked to the 2nd list). The items in both lists are part of the same class and both use the same constructor, so all of their parameters are the same. I have been looking around the internet and trying different things myself but i cant quite find the solution, up to now this is what I came up with, but i cant make it to work.

public void Gladiators_Data_Grid_CellContentClick(object sender, DataGridViewCellEventArgs e) { string ItemMoving =
Data_Grid.Rows[e.RowIndex].Cells[0].Value.ToString();

        var item = List1.FirstOrDefault(x => x.Name == ItemMoving);

        if (item != null)
        {
            List1.Remove(item);
            List2.Add(item);
        }
    }

The solution to my problem doesn't have to follow the pattern that I tried to use, anything that works will be highly appreciated, thanks in advance.

1

1 Answers

0
votes

1) if you are not allowing the user to reorder the items, use the index of the selected row. It will be much faster.

2) the rest of your process is correct. However, you forgot to rebind the datagrids to the new lists. The grids do not update their UI automatically.

Small pseudo example:

var item = List1[grid1.currentrow.index];

List1.Remove(item);
List2.Add(item);

Grid1.datasource=list1;
Grid2.datasource=list2;