پروژه ای معرفی کردید ، دقیقا از همین تکنیکی که اشاره کردم استفاده می کنه.
در حقیقت DataGrid محلی رو توسعه داده و از همون Eventی که پیشنهاد دادم استفاده کرده.
اما یه مشکل وجود داره و اونم اینه که اینDataGrid هم نیاز دوم شما (حرکت بوسیله کلید های جهت دار صفحه کلید) رو بر آورده نمی کنه و بایستی خودتون رویداد CurrentCellChanged رو براش پیاده سازی کنید
کدهای توسعه یافته رو ببینید :
Option Explicit On
Option Strict On
Imports System
Imports System.Windows
Imports System.Windows.Forms
Namespace Components
'''-----------------------------------------------------------------------------
''' Project : Project_DataGrid
''' Class : CDataGrid
'''
'''-----------------------------------------------------------------------------
''' <summary>
''' Inherited DataGrid
''' Now with new futures.
''' </summary>
''' <remarks>This class can not be inheritable.</remarks>
''' <history>
''' [jhabjan] 19.9.2003 Created
''' </history>
'''-----------------------------------------------------------------------------
Public NotInheritable Class CDataGrid
Inherits DataGrid
Private m_HtiMouseDown As HitTestInfo '//--- for storeing HitTestInfo
Private m_LastHTI As HitTestInfo '//--- lase selected row
Private m_ControlCollenction As New ArrayList '//--- backup of controlcollection
Private m_FullRowSelect As Boolean '//--- full row select
Sub New()
MyBase.New()
End Sub
'''-----------------------------------------------------------------------------
''' <summary>
''' We overrided event OnMouseDown so we can propertly set Grid.Select()
''' </summary>
''' <history>
''' [jhabjan] 19.9.2003 Created
''' </history>
'''-----------------------------------------------------------------------------
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim ptPoint As Point = New Point(e.X, e.Y)
'// first, call base event
MyBase.OnMouseDown(e)
Dim htInfo As HitTestInfo = Me.HitTest(ptPoint)
Try
'// check for cell type
If htInfo.Type = HitTestType.Cell Then
m_HtiMouseDown = htInfo
'// check if it will be fullrowselect
If m_FullRowSelect Then
'// set current cell
Me.CurrentCell = New DataGridCell(htInfo.Row, htInfo.Column)
Me.Select(htInfo.Row)
End If
End If
Catch
End Try
End Sub
'''-----------------------------------------------------------------------------
''' <summary>
''' We overrided event OnMouseUp so we can propertly set Grid.Select()
''' </summary>
''' <history>
''' [jhabjan] 19.9.2003 Created
''' </history>
'''-----------------------------------------------------------------------------
Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim ptPoint As Point = New Point(e.X, e.Y)
'// first, call base event
MyBase.OnMouseUp(e)
Dim htInfo As HitTestInfo = Me.HitTest(ptPoint)
Try
'// check for cell type
If htInfo.Type = HitTestType.Cell Then
m_LastHTI = htInfo
'// set current cell
Me.CurrentCell = New DataGridCell(htInfo.Row, htInfo.Column)
'// check if it will be fullrowselect
If m_FullRowSelect Then
'// unselect row that was selected on mouse down
Me.UnSelect(m_HtiMouseDown.Row)
'// select row on witch was mouse released (mouseup)
Me.Select(htInfo.Row)
End If
End If
Catch
End Try
End Sub
'''-----------------------------------------------------------------------------
''' <summary>
''' If we will have fullrowselect
''' </summary>
''' <history>
''' [jhabjan] 19.9.2003 Created
''' </history>
'''-----------------------------------------------------------------------------
<System.ComponentModel.Browsable(False)> _
Public Property FullRowSelect() As Boolean
Get
Return m_FullRowSelect
End Get
Set(ByVal Value As Boolean)
m_FullRowSelect = Value
If Value Then
If Not (m_LastHTI Is Nothing) Then
Me.Select(m_LastHTI.Row)
Else
Try
Me.Select(0)
Catch
End Try
End If
Else
If Not (m_LastHTI Is Nothing) Then
Me.UnSelect(m_LastHTI.Row)
Else
Try
Me.UnSelect(0)
Catch
End Try
End If
End If
End Set
End Property
End Class
'''-----------------------------------------------------------------------------
''' Project : Project_DataGrid
''' Class : CDataGridLabelColumn
'''
'''-----------------------------------------------------------------------------
''' <summary>
''' This is the way how to make column not editable.
''' In this case we just overrides sub 'Edit', and
''' let him do nothing. ( other word, brake on edit )
''' </summary>
''' <history>
''' [jhabjan] 20.9.2003 Created
''' </history>
'''-----------------------------------------------------------------------------
Public Class CDataGridLabelColumn
Inherits DataGridTextBoxColumn
Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
'// do nothing
End Sub
End Class
End Namespace