Add the right-click menu for DataGrid
Sunhai
Development Tools: Microsoft Visual Studio .NET 2003 operating system: Windows XP
Use XML as DataGrid data source
code show as below:
Dim filename as stringdim myXmldataset as new datasetFileName = "sunhai.xml" myXmldataset.readxml (filename) form1.definstance.DataGrid1.dataSource = MyXMLDataSet
Launcher, DataTable default is closed, you need to manually click to expand, not bull, use the following code to automatically expand DataTable:
Private Sub Form1_Load (Byval e AS Object, Byval e as system.eventargs "handles mybase.load data" Handles mybase.load DataGrid1.expand (-1) 'To expand the number of rows, set to -1 means expanded all rows DataGrid1.navigateto (0, "DataTablename ") End Sub
Get the coordinate of the DataGrid1 mouse
Dim rowNum, columnNum As Integer 'are the row and column numbers 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 (ex, EY) if E.Button = mousebuttons.right and e.clicks = 1 TEN 'If it is right-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 'right click row obtained columnNum = hti. Column 'gets the mouse right click on the listed End Select End if End Sub
Add ContextMenu
Add ContextMenu1: Text Name Remove a line MNUDELETEROW insert a line MNUNUNSertrow Set the ContextMenu in the DataGrid1 property to CONTEXTMENU1.
Private Sub Mnudeleterow_Click (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.Acceptchange () 'does not add this sentence, you will find that the inserted row is "last" how to insert the column in DataTable? Please advise!
April 27, 2004
My QQ: 26624998 My website: http://blog.9cbs.net/2066/