简体   繁体   中英

Delete a row from Datagridview c#

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,

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM