[VB.NET]VB.net中winForm的dataGrid如何只能选择单行
2008-12-29 22:31
399 查看
<script type="text/javascript"><!--
google_ad_client = "pub-8333940862668978";
/* 728x90, 创建于 08-11-30 */
google_ad_slot = "4485230109";
google_ad_width = 728;
google_ad_height = 90;
//-->
</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
__________________________________________________________________________
在程序中手动设置就可以了,我以前也遇到过!
__________________________________________________________________________
相关文章推荐
- [VB.NET]winform中如何在datagrid中使用复选框表示哪几行被选中
- [VB.NET]如何把.TXT的文件内容传到DATAGRID里面
- 如何隐藏或删除DataGrid的某列(VB.NET 2003)
- [VB.NET]急,如何用js获得,设置datagrid 的template column里面放的控件值?
- ext.net 日期控件如何指定控件只能选择当天以后的日期
- [VB.NET]如何做一个多语言(国际化)版本的winform
- [VB.NET]请教高手,如何选中datagrid中的行?
- WinForm的DataGrid在显示的时候,某个Cell得颜色要变成红的(VB.NET)
- vb.net 如何选择文件夹 不是文件
- [VB.NET]sql 如何选择某列之外的所有列?
- [VB.NET]请问如何在dataGrid中显示搜索结果?
- 我想在datagrid中选择若干记录,再点击添加按钮,给别一个ACCESS表添加选择的记录,在VB中如何实现?
- [VB.NET]winform下DataGrid导出excel问题
- [VB.NET]如何象在word中一样,通过工具栏中的组合框,选择字体/字号/颜色/大小等(详细点比较好)
- (转载)Asp.net中如何将DataSet写入xml文件,及如何将xml读出来绑定到DataGrid 选择自 kgdiwss 的 Blog
- c# winform中如何判断DataGrid当前选中的是空白行,并根据选择控制按钮是否可用
- 如何在Visual Studio C#.Net WinForm 中 TreeView中右键选择节点进行处理
- [VB.NET]如何删除datagrid选中的行
- 如何使用 Visual VB .NET 对 DataGrid Windows 控件执行分页
- WinForm中把DataGrid里面的数据导出成EXCEL文件(VB.net)