Add right menu to DataGrid

Source: Internet
Author: User
The datagrid| menu code is as follows:

Dim FileName as String
Dim Myxmldataset as New DataSet
FileName = "Sunhai.xml"
Myxmldataset.readxml (FileName)
Form1.DefInstance.DataGrid1.DataSource = Myxmldataset


Start the program, the DataTable default is closed, need to manually click the expansion, annoying, with the following code to implement automatic expansion of the DataTable:

Private Sub Form1_Load (ByVal sender as Object, ByVal e as System.EventArgs) Handles MyBase.Load

Datagrid1.expand (-1) ' Number of rows to expand, set to-1 to expand all rows
Datagrid1.navigateto (0, "dataTableName")

End Sub




   

  
Get the coordinates of the right mouse click on the DATAGRID1


Dim rownum, columnnum as Integer ' is the line number and column number, respectively
Private Sub Datagrid1_mousedown (ByVal sender as Object, ByVal e as System.Windows.Forms.MouseEventArgs) Handles DATAGRID1 . MouseDown

Dim Mygrid as DataGrid = CType (sender, DataGrid)
Dim Hti as System.Windows.Forms.DataGrid.HitTestInfo
Hti = Mygrid.hittest (e.x, e.y)

If E.button = mousebuttons.right and e.clicks = 1 Then ' If the right mouse click
Select case Hti. Type '
Case System.Windows.Forms.DataGrid.HitTestType.Cell, System.Windows.Forms.DataGrid.HitTestType.RowHeader, System.Windows.Forms.DataGrid.HitTestType.ColumnHeader

RowNum = Hti. Row ' Get the right mouse click in the row
Columnnum = Hti. Column ' Get the right mouse click
End Select

End If

End Sub


                 

Add ContextMenu

Add ContextMenu1 in design mode:
Text Name
Delete a row Mnudeleterow
Insert a row Mnuinsertrow

Set the ContextMenu in the DataGrid1 property to ContextMenu1.

Private Sub Mnudeleterow_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Mnudeleterow.click

Myxmldataset.tables (0). Rows.removeat (rownum) ' Delete row

End Sub




Private Sub Mnuinsertrow_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Mnuinsertrow.click

Dim row1 as DataRow = Myxmldataset.tables (0). NewRow
Myxmldataset.tables (0). Rows.insertat (Row1, rownum)
Myxmldataset.acceptchanges () ' Without this, you'll find that the inserted line is "final."

End Sub



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.