0
votes

I have 2 datagridviews.

datagridview 1 - I have item name, item price, add/remove button ( a toggle btn).

datagridview 2 - The items added from dgv1 will be populated in dgv2.

When remove button from dgv1 is clicked, the added item should be removed from dgv2.

How to achieve this ?

Thanks,

1
You can use Rows.Remove or Rows.RemoveAt methodsvivek nuna

1 Answers

0
votes

If you put your rows into a DataTable then life would be very easy:

var dt = new DataTable();
dt.Columns.Add("Text");
dt.Columns.Add("IsInGrid2", typeof(bool));

datagridView1.DataSource = dt;

var dv  = new DataView(dt);
dv.RowFilter = "[IsInGrid2] = True";
datagridView2.DataSource = dv;

dt.Rows.Add("I'm in grid 1 only", false);
dt.Rows.Add("I'm in both grids", true);

Now if you toggle the boolean in the row, it will/not appear in grid2 accordingly. To remove a row from both grids, delete it from the table:

//make both rows appear in the grid 2
dt.Rows[0]["IsInGrid2"] = true;

//remove first row from both grids:
dt.Rows.RemoveAt(0);