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

前端之家收集整理的这篇文章主要介绍了[VB.NET]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 __________________________________________________________________________ 在程序中手动设置就可以了,我以前也遇到过! __________________________________________________________________________ 原文链接:https://www.f2er.com/vb/263453.html

猜你在找的VB相关文章