c# - How to filter / selectively copy values from one DataGridView to another DataGridView -


i have 2 datagridviews: productsdatagridview , promotionsdatagridview.

the first one, productsdatagridview, reads all values file method:

public static list<products> loaduserlistfromfile(string filepath) {     var loadproductsdata = new list<products>();      foreach (var line in file.readalllines(filepath))     {         var columns = line.split('\t');         loadproductsdata.add(new products         {             inventoryid = "bg" + columns[0],             brand = columns[1],             category = columns[2],             description = columns[3],             promotions = convert.toint32(columns[4]),             quantity = convert.toint32(columns[5]),             price = convert.todouble(columns[6])         });     }      return loadproductsdata; } 

the first datagridview (productsdatagridview) filled correctly values. in productsdatagridview have set-up check-box column called "promotion" (column promotion reads integer values file): if has value of 0 - box not checked, if greater 1: checked. want filter/move (i don't care of both exactly) values productsdatagridview promotionsdatagridview have >0 value in check-box column (promotions).

enter image description here

example: if productsdatagridview has 25 total products, 8 promotional products (have value >0 in check-box column), promotionsdatagridview should filled 8 values, copied/moved/filtered/whatever datagridview.

so far can copy data first datagridview second 1 following code:

public void experimental2() {     datagridview1.datasource = products.loaduserlistfromfile(filepath);     //bind datagridview linq      var dg1 =         (from in productsdatagridview.rows.cast<datagridviewrow>()          select new { column1 = a.cells["column1"].value.tostring() }).tolist();      //loop dg1 , save datagridview2     foreach (var b in dg1)     {         datagridview1.rows.add(b.column1);     } } 

i made few pitiful attempts insert if check, job me (copy if columnt[4] > 0) new datagridview couldn't write compile @ all...

please, me!

if both grids have same schema (and assume have) going find rows checked, product bound given row, create new result list , bind next grid.

var results = new list<products>(); //our new data source checked items  foreach (datagridviewrow row in productsdatagridview.rows) {     var item = row.databounditem products; //get product row (only when grid databound!)      if (item.promotions > 0)     {         results.add(item);             } }  promotionsdatagridview.datasource = results;  

if want delete rows first grid checked create temporary list of rows, add checked rows , @ end iterate on them , remove 1 one. hope out :)


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -