Dev gridcontrol (2) bind a value control to the drag and order function, serial number

Source: Internet
Author: User

The sorting function cannot be implemented after a long time. Finally, I wrote a method to implement it.

When binding data, use the generic type. When dragging the data, you can obtain the selected row and the row number on the top row, and then use the following method.

 

Register an event

 

Protected Override Void Onload (eventargs E)
{
Base . Onload (E );
// Add event
This . Gridcontrol1.dragover + = New System. Windows. Forms. drageventhandler ( This . Gridcontrolincludragover );
This . Gridcontrol1.dragdrop + = New System. Windows. Forms. drageventhandler ( This . Gridcontrolincludragdrop );
This . Gridcontrol1.mousedown + = New Mouseeventhandler (gridcontrol+mousedown );
This . Gridcontrol1.mousemove + = New Mouseeventhandler (gridcontrol+mousemove );
Gridcontrol1.allowdrop = True ;
Bindgrid ( Null );

}

 

 

 

Bind Value

///   <Summary>
/// Bind data
///   </Summary>
///   <Param name = "modellist"> </param>
Private Void Bindgrid (list <ordermodel> modellist)
{
If (Modellist = Null )
{
Modellist = New List <ordermodel> ();
String Sqlstr = " Select functionid, functiontitle, loadorder from evbase_functionlist where parentid = " + Parentid + " Order by loadorder ASC " ;
Datatable dt = mnscevdbmanager. getdatatable (sqlstr, Null );
Foreach (Datarow Dr In DT. Rows)
{
Ordermodel model = New Ordermodel () {id = Dr [ " Functionid " ]. Tostring (), Title = Dr [ " Functiontitle " ]. Tostring (), loadorder = Dr [ " Loadorder " ]. Tostring ()};
Modellist. Add (model );
}

}
This. Gridcontrol1.datasource = modellist;
This. Gridcontrol1.refreshdatasource ();
}

 

 

Drag event

 

# Region
// <Gridcontrol1>
Private Void Gridcontrol+mousedown ( Object Sender, system. Windows. Forms. mouseeventargs E)
{
Hitinfo = gridview1.calchitinfo ( New Point (E. X, E. y ));
If (Hitinfo. rowhandle < 0 ) Hitinfo = Null ;
}

Private Void Gridcontrol1_mousemove ( Object Sender, system. Windows. Forms. mouseeventargs E)
{
If (Hitinfo = Null ) Return ;
If (E. Button! = Mousebuttons. Left) Return ;
Rectangle dragrect = New Rectangle ( New Point (
Hitinfo. hitpoint. X-systeminformation. dragsize. width/ 2 ,
Hitinfo. hitpoint. Y-systeminformation. dragsize. Height/ 2 ), Systeminformation. dragsize );
If (! Dragrect. Contains ( New Point (E. X, E. y )))
{
If (Hitinfo. inrow)
{
Gridcontrol1.dodragdrop ( New Object [] {Hitinfo. rowhandle, gridcontrol1}, dragdropeffects. All );
}
}
}
// </Gridcontrol1>

Private Int Getdragobject ( Object Sender, idataobject data)
{
Object [] OBJ = data. getdata ( Typeof ( Object []) As Object [];
If (OBJ = Null |! Sender. Equals (gridcontrol1 )) Return - 2 ;
If (OBJ [ 0 ] Is Int32)
Return ( Int ) OBJ [ 0 ];
Else Return - 1 ;
}

// <Gridcontrol1>
Private Void Gridcontrolincludragover ( Object Sender, system. Windows. Forms. drageventargs E)
{
Gridhitinfo HI = gridview1.calchitinfo (gridcontrol1.pointtoclient ( New Point (E. X, E. y )));
Int Handle = getdragobject (sender, E. data );
If (Hi. inrow & handle> = 0 & Handle! = Hi. rowhandle)
{
If (E. keystate & 8 )! = 0 )
E. effect = dragdropeffects. copy;
Else
E. effect = dragdropeffects. move;
}
Else E. effect = dragdropeffects. None;
}

private void gridcontrolincludragdrop ( Object sender, system. windows. forms. drageventargs e)
{< br> var templist = gridview1.datasource as List ;
int currentid = getdragobject (sender, e. data);

Gridhitinfo HI = gridview1.calchitinfo (gridcontrol1.pointtoclient (NewPoint (E. X, E. y )));
IntNewhandle = Hi. rowhandle;
//Int result = currentid-newhandle;
Ordermodel model = templist [currentid];

Templist. removeat (currentid );
// Templist. Add (model );
Templist. insert (newhandle, model );
Bindgrid (templist );
}
// </Gridcontrol1>

Private Void Gridviewappsshowingeditor ( Object Sender, system. componentmodel. canceleventargs E)
{
If (! Mousebuttons. Equals (mousebuttons. None ))
E. Cancel = True ;
}
# Endregion

 

 

Show serial number

 

Void Gridview1_mdmdrawrowindicator ( Object Sender, devexpress. xtragrid. Views. Grid. rowindicatorcustomdraweventargs E)
{
If (E. info. isrowindicator & E. rowhandle> = 0 )
{
E. info. displaytext = (E. rowhandle + 1 ). Tostring ();
}
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.