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

VB.NET源码-156个实用实例哦…… <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
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
__________________________________________________________________________
在程序中手动设置就可以了,我以前也遇到过!
__________________________________________________________________________
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值