[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 __________________________________________________________________________ 在程序中手动设置就可以了,我以前也遇到过! __________________________________________________________________________

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...