代码如下:
Dim filename As StringDim MyXmlDataSet As New DataSetfilename = "sunhai.xml"MyXmlDataSet.readxml(filename)Form1.DefInstance.DataGrID1.DataSource = MyXmlDataSet
启动程序,Datatable默认是闭合的,需要手动点击展开,不胜其烦,用如下代码实现自动展开Datatable:
Private Sub Form1_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles MyBase.Load DataGrID1.Expand(-1) '要展开的行数,设为-1表示展开所有行 DataGrID1.Navigateto(0,"Datatablename") End Sub
获得在DataGrID1鼠标右击的座标
Dim rowNum,columnNum As Integer '分别是行号和列号 Private Sub DataGrID1_MouseDown(ByVal sender As Object,ByVal e As System.windows.Forms.MouseEventArgs) Handles DataGrID1.MouseDownDim myGrID As DataGrID = CType(sender,DataGrID)Dim hti As System.windows.Forms.DataGrID.HitTestInfohti = myGrID.HitTest(e.X,e.Y)If e.button = Mousebuttons.Right And e.Clicks = 1 Then '如果是鼠标右击 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 '获得鼠标右击所在行 columnNum = hti.Column '获得鼠标右击所在列 End Select End IfEnd Sub
添加ContextMenu
在设计模式添加ContextMenu1:
Text name
删除一行 mnuDeleteRow
插入一行 mnuInsertRow
把DataGrID1属性中的ContextMenu设为ContextMenu1。
Private Sub mnuDeleteRow_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles mnuDeleteRow.Click MyXmlDataSet.tables(0).Rows.RemoveAt(rowNum) '删除行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() '不加这句,你会发现所插入行都到“最后”了
End Sub
以上是内存溢出为你收集整理的VB.NET 给DataGrid加上右键菜单全部内容,希望文章能够帮你解决VB.NET 给DataGrid加上右键菜单所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)