c# - Drag and Drop Row in a DataGridView -


i have windows form datagridview , need enable drag , drop rows in datagridview.

it filled database. code not work because after first drag , drop cant drop row right position.

this load forms datagridview filled database

datatable bspeople; rectangle dragboxfrommousedown;     int rowindexfrommousedown;     int rowindexofitemundermousetodrop;      private void form1_load(object sender, eventargs e)     {         datagridview1.allowdrop = true;         bspeople= objpeople.returnpeople(); // fill data sql server          datagridview1.datasource = bspeople;     } 

this drag , drop events

private void datagridview1_mousemove(object sender, mouseeventargs e)     {         if (((e.button ==mousebuttons.left)))         {             if (((dragboxfrommousedown != rectangle.empty)                         && !dragboxfrommousedown.contains(e.x, e.y)))             {                 dragdropeffects dropeffect = datagridview1.dodragdrop(datagridview1.rows[rowindexfrommousedown], dragdropeffects.move);             }         }     }      private void datagridview1_mousedown(object sender, mouseeventargs e)     {         rowindexfrommousedown = datagridview1.hittest(e.x, e.y).rowindex;         if ((rowindexfrommousedown != -1))         {             size dragsize = systeminformation.dragsize;             dragboxfrommousedown = new rectangle(new point(e.x - (dragsize.width / 2), e.y - (dragsize.height / 2)), dragsize);         }         else         {             dragboxfrommousedown = rectangle.empty;         }     }      private void datagridview1_dragover(object sender, drageventargs e)     {         e.effect = dragdropeffects.move;     }      private void datagridview1_dragdrop(object sender, drageventargs e)     {         point clientpoint = datagridview1.pointtoclient(new point(e.x, e.y));         rowindexofitemundermousetodrop = datagridview1.hittest(clientpoint.x, clientpoint.y).rowindex;         if ((e.effect == dragdropeffects.move))         {             datagridviewrow rowtomove = (datagridviewrow)e.data.getdata(typeof(datagridviewrow));             object[] celldata=new object[datagridview1.columncount];             (int col = 0; (col                         <= (rowtomove.cells.count - 1)); col++)             {                 celldata[col] = rowtomove.cells[col].value;             }              datarow row = bspeople.newrow();             row.itemarray = celldata;             bspeople.rows.insertat(row, rowindexofitemundermousetodrop);             rowtomove.datagridview.rows.remove(rowtomove);          }     } } 

drag & drop

you need delete , insert right datarows. try this:

private void dgv_dragdropdata(object sender, drageventargs e) {    point clientpoint = dgv.pointtoclient(new point(e.x, e.y));    rowindexofitemundermousetodrop =        dgv.hittest(clientpoint.x, clientpoint.y).rowindex;    if (e.effect == dragdropeffects.move)    {       datagridviewrow rowtomove = (datagridviewrow)e.data.getdata(typeof(datagridviewrow));       // find row move in datasource:       datarow oldrow = ((datarowview)rowtomove.databounditem).row;       // clone it:       datarow newrow = bspeople.newrow();       newrow.itemarray = oldrow.itemarray;       bspeople.rows.insertat(newrow, rowindexofitemundermousetodrop);       bspeople.rows.remove(oldrow);    } } 

note code moves one row. moving multiple rows there few more tricks needed..

state of row:

also note may need set rowstate of new row, depending on needs. added may want set it unchanged calling newrow.acceptchanges() or maybe other state, depending on state of original row.

to modify rowstate few rules apply:

  • the datarow must reside in datatable; after have created it, state unattached , can't change it.
  • only unchanged rows can changed setadded or setmodified methods; must call acceptchanges() first.

this should job:

   if (oldrow.rowstate != datarowstate.unchanged) newrow.acceptchanges();    if (oldrow.rowstate == datarowstate.added) newrow.setadded();    if (oldrow.rowstate == datarowstate.modified) newrow.setmodified(); 

these lines must added after new row added table..


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -