c# - Copying A SINGLE SELECTED row from a datagridview to another datagridview -
what need : 1. when user clicks on row of datagriview. row displayed in datagridview (so can edited etc...)
note : grid_display = need row. note : grid_detail = need single row copied.
what have till now:
foreach (datagridviewcolumn c in grid_display.columns) { grid_detail.columns.add(c.clone() datagridviewcolumn); } //then can copy rows values 1 one (working on selectedrows collection) foreach (datagridviewrow r in grid_display.selectedrows) { int index = grid_detail.rows.add(r.clone() datagridviewrow); foreach (datagridviewcell o in r.cells) { grid_detail.rows[index].cells[o.columnindex].value = o.value; } } }
i able make new row. not feed in selected row in it. ?
as mentioned in comment already, should use datasource
datagridview
s , use datagridview
view only.
here have complete example demonstrates how use datatable
datasource
datagridview
, move datarow
between them.
- creata new windows forms project , copy paste code.
- click on row, clicking left row (indicated small arrow > ) in
datagridview
- click on
button
belowdatagridview
move selecteddatarow
otherdatagridview
note 1: controls crated in form1_load dont bother drag dropping controls on form.
note 2: if have column selected, no rows moved. have select entire row.
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; namespace windowsformsapplication1 { public partial class form1 : form { public form1() { initializecomponent(); this.load +=form1_load; } public datagridview viewa, viewb; public datatable dataa, datab; public button moveselectedfromviewatob, moveselectedfromviewbtoa; private void form1_load(object sender, eventargs e) { //create 2 datagridviews on form viewa = new datagridview() { location = new point(0, 0), size = new size(300, 100), multiselect = false }; viewb = new datagridview() { location = new point(300, 0), size = new size(300, 100), multiselect = false }; this.controls.add(viewa); this.controls.add(viewb); //add 2 buttons button moveselectedfromviewatob = new button() { text = "a => b", location = new point(10, 120), }; moveselectedfromviewatob.click += moveselectedfromviewatob_click; moveselectedfromviewbtoa = new button() { text = "a <= b", location = new point(310, 120) }; moveselectedfromviewbtoa.click += moveselectedfromviewbtoa_click; this.controls.add(moveselectedfromviewatob); this.controls.add(moveselectedfromviewbtoa); //make sure form has appropriate size this.size = new size(600, 200); //create datatable , add data dataa = new datatable(); dataa.columns.add("key", typeof(system.string)); dataa.columns.add("value", typeof(system.string)); dataa.rows.add(new object[] { "keya", "valuea" }); dataa.rows.add(new object[] { "keyb", "valueb" }); dataa.rows.add(new object[] { "keyc", "valuec" }); dataa.rows.add(new object[] { "keyd", "valued" }); //make sure datab has same layout dataa datab = dataa.clone(); //assign both datatables views viewa.datasource = new bindingsource() { datasource = dataa }; viewb.datasource = new bindingsource() { datasource = datab }; } /// <summary> /// moves rows view b /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void moveselectedfromviewatob_click(object sender, eventargs e) { if (viewa.selectedrows.count == 0 || dataa.rows.count == 0 || viewa.selectedrows[0].index > dataa.rows.count -1) return;//no row selected, or data table empty datab.importrow(dataa.rows[viewa.selectedrows[0].index]); dataa.rows.removeat(viewa.selectedrows[0].index); } /// <summary> /// moves rows view b /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void moveselectedfromviewbtoa_click(object sender, eventargs e) { if (viewb.selectedrows.count == 0 || datab.rows.count == 0 || viewb.selectedrows[0].index > datab.rows.count -1) return; //no row selected, or data table empty dataa.importrow(datab.rows[viewb.selectedrows[0].index]); datab.rows.removeat(viewb.selectedrows[0].index); } } }
with should able implement logic of when move row grid_detail
, when move grid_display
.
Comments
Post a Comment