加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

[VB.NET]VB.net中winForm的dataGrid如何只能选择单行

发布时间:2020-12-16 23:38:00 所属栏目:大数据 来源:网络整理
导读:VB.net中winForm的dataGrid如何只能选择单行 VB.net中winForm的dataGrid如何只能选择单行 __________________________________________________________________________ VB.net中winForm的dataGrid如何只能选择单行 ______________________________________
VB.net中winForm的dataGrid如何只能选择单行 VB.net中winForm的dataGrid如何只能选择单行 __________________________________________________________________________ VB.net中winForm的dataGrid如何只能选择单行 __________________________________________________________________________ 将multiSelect属性设置为false __________________________________________________________________________ 我是.net2003,datagrid中没有multiSelect这个属性啊 __________________________________________________________________________ 看这个,继承的DataGrid,只选中一行呵呵。这是C#的,你可以改写成VB的,或者写到类中,调用。 How can I make my DataGrid support a single select mode,and not the default multiselect mode? One way to do this is to derive a DataGrid,override its OnMouseDown and OnMouseMove methods. In the OnMouseDown,handle selecting and unselecting in your code without calling the base class if the click is on the header. In the OnMouseMove,don t call the baseclass to avoid dragging selections. Below is a code snippet for a sample derived DataGrid. You can download a full project (C#,VB). public class MyDataGrid : DataGrid { private int oldSelectedRow = -1; protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e) { //don t call the base class if left mouse down if(e.Button != MouseButtons.Left) base.OnMouseMove(e); } protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e) { //don t call the base class if in header DataGrid.HitTestInfo hti = this.HitTest(new Point(e.X,e.Y)); if(hti.Type == DataGrid.HitTestType.Cell) { if(oldSelectedRow > -1) this.UnSelect(oldSelectedRow); oldSelectedRow = -1; base.OnMouseDown(e); } else if(hti.Type == DataGrid.HitTestType.RowHeader) { if(oldSelectedRow > -1) this.UnSelect(oldSelectedRow); if((Control.ModifierKeys & Keys.Shift) == 0) base.OnMouseDown(e); else this.CurrentCell = new DataGridCell(hti.Row,hti.Column); this.Select(hti.Row); oldSelectedRow = hti.Row; } } } __________________________________________________________________________ VB版,写到一个类文件中就能用了。 Option Strict Off Option Explicit On Imports Microsoft.VisualBasic Imports System Imports System.Drawing Imports System.Windows.Forms Public Class MyDataGrid Inherits DataGrid Private oldSelectedRow As Integer Fields Constructors Events Methods Public Sub New() Warning: Implementation not found End Sub Protected Overloads Overrides Sub OnMouseMove(ByVal e As MouseEventArgs) don t call the base class if left mouse down If (e.Button <> Windows.Forms.MouseButtons.Left) Then MyBase.OnMouseMove(e) End If End Sub Protected Overloads Overrides Sub OnMouseDown(ByVal e As MouseEventArgs) don t call the base class if in header Dim hti As DataGrid.HitTestInfo hti = Me.HitTest(New Point(e.X,e.Y)) If (hti.Type = DataGrid.HitTestType.Cell) Then If (oldSelectedRow > -(1)) Then Me.UnSelect(oldSelectedRow) End If oldSelectedRow = -(1) MyBase.OnMouseDown(e) Else If (hti.Type = DataGrid.HitTestType.RowHeader) Then If (oldSelectedRow > -(1)) Then Me.UnSelect(oldSelectedRow) End If If ((Control.ModifierKeys And Keys.Shift) _ = 0) Then MyBase.OnMouseDown(e) Else Me.CurrentCell = New DataGridCell(hti.Row,hti.Column) End If Me.Select(hti.Row) oldSelectedRow = hti.Row End If End If End Sub End Class __________________________________________________________________________ 在程序中手动设置就可以了,我以前也遇到过! __________________________________________________________________________

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读