Make the datagridview support dragdrop)

Source: Internet
Author: User

When creating a Custom User component, you can drag and drop between the Treeview component and the datagridview component. The datagridview is used as the target object for drag and drop. I originally wanted to find ready-made code snippets on the network, but unfortunately I found almost all the code that dragged and dropped data from the datagridview to other components. Alas ~~, It seems that it is not easy to be lazy, so you have to study it yourself. Fortunately, it's not complicated. I quickly found the method. Well, I don't need to talk about it. I will post the code snippet for everyone to share.

Itemdrag code of the node drag-and-drop event supported by the Treeview component as the source object:

Private void tvmetadata_itemdrag (Object sender, itemdrageventargs E)
{
If (E. Button. Equals (mousebuttons. Left) & (treenode) E. Item). nodes. Count = 0)
{
Tvmetadata. dodragdrop (E. Item, dragdropeffects. Move );
}
}
It's very simple. You can determine whether to press the left mouse button, and only allow drag and drop of the bottom node, and then set dragdropeffects.

Dragenter event processing of the target object datagridview:

Private void dtcolumns_dragenter (Object sender, drageventargs E)
{
// Only supports drag and drop of Tree nodes
If (E. Data. getdatapresent (typeof (treenode )))
{
E. effect = dragdropeffects. move;
}
}

Needless to say, use dragenter to determine whether the drag-and-drop operation is acceptable;

Next is the main data receiving program segment:

Private void dtquerycondition_dragdrop (Object sender, drageventargs E)
{
// Convert the screen coordinates to the control coordinates and then obtain the rows and columns of the datagridview .
Point clientpt = dtquerycondition. pointtoclient (new point (E. X, E. y ));
System. Windows. Forms. datagridview. hittestinfo ht = dtquerycondition. hittest (clientpt. X, clientpt. y );
If (HT. columnindex. Equals (0 ))
{
Sqlcolumn SC = (sqlcolumn) (treenode) E. Data. getdata (typeof (treenode). Tag ));
If (bsfiltecondition. Count = 0 | HT. rowindex> (bsfiltecondition. Count-1 ))
{
Querycondition qc = new querycondition ();
QC. fieldname = SC. tablename + "." + SC. Name;
Bsfiltecondition. Add (QC );
}
Else
{
Dtquerycondition. currentcell = dtquerycondition. Rows [Ht. rowindex]. cells [Ht. columnindex];
Dtquerycondition. beginedit (false );
Dtquerycondition. currentcell. value = SC. tablename + "." + SC. Name;
Dtquerycondition. endedit ();
}
}

}

Function Description: if a new row is added, a new record is added to the data source of the datagridview (I use bindingsource). If an existing row is modified, by setting currentcell, beginedit, and endedit, set currentcell as the determined cell when you drag and drop the mouse. Then call beginedit and endedit to endedit the cell data set by the drag and drop operation on the datagridview.

Well ~~~~, It's easy.

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.