Datagridview
Right-click on the dview and select the current row from the shortcut menu.
Private Void Maid ( Object Sender, datagridviewcellmouseeventargs E)
{
Datagridview1.clearselection ();
Datagridview1.rows [E. rowindex]. Selected= True;
Datagridview1.currentcell=Datagridview1.rows [E. rowindex]. cells [E. columnindex];
}
/**/ /*
By Hello [unregistered user]
Use the "Maid cellmouseclick" Event
Sometimes, only the menu is displayed, but no cell is selected. sometimes only the cell is selected, and no menu is displayed.
If the menu control is bound to the datagridview (datagridview1.contextmenustrip = This. contextsrtip1), you cannot select a cell.
*/
Datagridview hyperlink Click Event
// Click content and determine the column name
Private Void Dgvdata_cellcontentclick ( Object Sender, datagridviewcelleventargs E)
{
Datagridview dgv = Sender As Datagridview;
String Columnname = Dgv. Columns [E. columnindex]. Name;
If ( " Operation " . Equals (columnname ))
{//If the column name is correct
Dgv. Rows. removeat (E. rowindex );
}
}
Treeview
C # implement the drag function in the winform Treeview Control
Node drag event # Region Node drag event
// When you start to drag a node
Private Void Tvmodel_itemdrag ( Object Sender, itemdrageventargs E)
{< br> treenode selectnode = E. item as treenode;
This . tvmodel. selectednode = selectnode;
This . form. dodragdrop (E. item, dragdropeffects. move);
}
// When you drag an object into the control's boundary
Private Void Tvmodel_dragenter ( Object Sender, drageventargs E)
{
Treenode enternode = (Treenode) (E. Data. getdata ( Typeof (Treenode )));
If (Enternode ! = Null )
E. Effect = Dragdropeffects. move;
Else
E. Effect = Dragdropeffects. None;
}
// When the drag-and-drop operation is completed
Private Void Tvmodel_dragdrop ( Object Sender, drageventargs E)
{
Treenode selectnode = (Treenode) (E. Data. getdata ( Typeof (Treenode )));
// 0. Exit if the source is empty or not a secondary Node
If (Selectnode = Null | Selectnode. Level ! = 1 )
Return ;
// 1. Obtain the target node based on the mouse coordinates
Position. x = E. X;
Position. Y = E. Y;
Position = This . Tvmodel. pointtoclient (position );
Treenode targetnode = This . Tvmodel. getnodeat (position );
Foreach (Treenode Node In This . Tvmodel. nodes [ 0 ]. Nodes)
Node. nodefont = New Font ( This . Form. Font, fontstyle. Regular );
// 2. Obtain the index of the target node to be inserted.
Int Index = 0 ;
If (Targetnode = Null )
Index = Selectnode. Parent. nodes. Count - 1 ; // The target node is null and is directly inserted to the end.
Else If (Targetnode. Level = 1 )
Index = Targetnode. Index + 1 ; // The target node is at the same level and is directly inserted later.
Else If (Targetnode. Level = 0 )
Index = 0 ; // The target is the root node and is directly inserted to the beginning.
Else If (Targetnode. Level > 1 )
{ // After the target node is a second-level node, It is traversed to its second-level node.
While (Targetnode. Level > 1 )
Targetnode = Targetnode. parent;
Index = Index = Targetnode. Index + 1 ;
}
// 3. Delete the source node. Insert the target node in the index and highlight it.
Selectnode. Remove ();
This . Tvmodel. nodes [ 0 ]. Nodes. insert (index, selectnode );
This . Tvmodel. selectednode = Selectnode;
}
// When dragging an object over the control edge
Private Void Tvmodel_dragover ( Object Sender, drageventargs E)
{
Position. x = E. X;
Position. Y = E. Y;
Position = This . Tvmodel. pointtoclient (position );
// Dashes are displayed before and after the dragged control.
Treenode targetnode = This . Tvmodel. getnodeat (position );
If (Targetnode ! = Null && Targetnode. Level = 1 )
{
If (Targetnode. prevnode ! = Null )
Targetnode. prevnode. nodefont = New Font ( This . Form. Font, fontstyle. Regular );
If (Targetnode. nextnode ! = Null )
Targetnode. nextnode. nodefont = New Font ( This . Form. Font, fontstyle. Regular );
Targetnode. nodefont = New Font ( This . Form. Font, fontstyle. Underline );
}
}
# Endregion