winforms - Move selected row from DataGridView to another in Visual C# -
i created form 2 datagridviews. 1 of these datagridviews filled data database , source. second datagridview should filled selected rows source datagridview after use button.
the first step if fill datatable filled :
public datatable loadmatimptable(string query) { myconn.open(); sqlitecommand cmd = new sqlitecommand(query, myconn); sqlitedataadapter sda = new sqlitedataadapter(); sda.selectcommand = cmd; datatable dt= new datatable(); sda.fill(dt); return dt; }
after fill source datagridview:
datatable dt = lt.loadmatimptable(querymat); this.matexpdatagridvw.rows.clear(); foreach (datarow item in dt.rows) { int n = matexpdatagridvw.rows.add(); matexpdatagridvw.rows[n].cells[0].value = false; matexpdatagridvw.rows[n].cells[1].value = item["materialid"].tostring(); matexpdatagridvw.rows[n].cells[2].value = item["name"].tostring(); matexpdatagridvw.rows[n].cells[3].value = item["preis"]; matexpdatagridvw.rows[n].cells[4].value = item["anzahl"].tostring(); matexpdatagridvw.rows[n].cells[5].value = item["datum"].tostring(); }
and push "exportbutton" , selected rows copied second datagridview. don't wont't copy of rows in second datagridview. move selected rows. tried in remove of items in foreach loop:
private void mvimpselectionbt_click(object sender, eventargs e) { foreach (datagridviewrow item in matexpdatagridvw.rows) { if ((bool)item.cells[0].value == true) { int n = matimpdatagridvw.rows.add(); matimpdatagridvw.rows[n].cells[0].value = false; matimpdatagridvw.rows[n].cells[1].value = item.cells[1].value.tostring(); matimpdatagridvw.rows[n].cells[2].value = item.cells[2].value.tostring(); matimpdatagridvw.rows[n].cells[3].value = item.cells[3].value.tostring(); matimpdatagridvw.rows[n].cells[4].value = item.cells[4].value.tostring(); matimpdatagridvw.rows[n].cells[5].value = item.cells[5].value.tostring(); } } #delete selected rows foreach (datagridviewrow item in matexpdatagridvw.selectedrows) { matexpdatagridvw.rows.remove(item); } }
but if tried deletion in way. selected rows copied last selected row deleted in source datgridview. whats best way delete selected rows?
create list of rows while copying, , use list basis deleting rows source.
private void mvimpselectionbt_click(object sender, eventargs e) { list<datarow> rowstodelete = new list<datarow>(); foreach (datagridviewrow item in matexpdatagridvw.rows) { if ((bool)item.cells[0].value == true) { //copy row int n = matimpdatagridvw.rows.add(); matimpdatagridvw.rows[n].cells[0].value = false; matimpdatagridvw.rows[n].cells[1].value = item.cells[1].value.tostring(); matimpdatagridvw.rows[n].cells[2].value = item.cells[2].value.tostring(); matimpdatagridvw.rows[n].cells[3].value = item.cells[3].value.tostring(); matimpdatagridvw.rows[n].cells[4].value = item.cells[4].value.tostring(); matimpdatagridvw.rows[n].cells[5].value = item.cells[5].value.tostring(); //add list rowstodelete.add(item); } } foreach(datarow row in rowstodelete) { matexpdatagridvw.rows.remove(row); } }
another way this, 1 loop, use loop iterator instead of foreach loop.
for (int = matexpdatagridvw.rows.count - 1; >= 0; i--) { if ((bool)matexpdatagridvw.rows[i].cells[0].value == true) { //copy row int n = matimpdatagridvw.rows.add(); matimpdatagridvw.rows[n].cells[0].value = false; matimpdatagridvw.rows[n].cells[1].value = matexpdatagridvw.rows[i].cells[1].value.tostring(); matimpdatagridvw.rows[n].cells[2].value = matexpdatagridvw.rows[i].cells[2].value.tostring(); matimpdatagridvw.rows[n].cells[3].value = matexpdatagridvw.rows[i].cells[3].value.tostring(); matimpdatagridvw.rows[n].cells[4].value = matexpdatagridvw.rows[i].cells[4].value.tostring(); matimpdatagridvw.rows[n].cells[5].value = matexpdatagridvw.rows[i].cells[5].value.tostring(); //delete row matexpdatagridvw.rows[i].delete(); } matexpdatagridvw.acceptchanges(); }
Comments
Post a Comment