'DataGridコントロールには動的にスクロールを変化させるメソッドがないため、
'継承して新しいコントロールの作成
'It succeeds and DataGrid control is created.
Public Class CustomDataGrid
    Inherits DataGrid
    Public headerLine As Boolean = False

    'DataGridの先頭に表示される行を設定する
    'The line displayed at the head of DataGrid is set up.
    Public Sub SetTopRow(ByVal rowNum As Integer)
        Dim args As New ScrollEventArgs( _
            ScrollEventType.LargeIncrement, rowNum)
        GridVScrolled(Me, args)
    End Sub

    'DataGridの先頭に表示される行を取得する
    Public Function GetTopRow() As Integer
        Return VertScrollBar.Value
    End Function

    '横向きのスクロールを設定する
    'Sideways scrolling is set up.
    Public Sub HScrollSet(ByVal rowNum As Integer)
        Dim args As New ScrollEventArgs( _
                   ScrollEventType.LargeIncrement, rowNum)
        gridhscrolled(Me, args)
    End Sub
    'DataGridの横スクロールを取得します
    'Horizontal scrolling of DataGrid is acquired.
    Public Function getHScroll() As ScrollBar
        Return HorizScrollBar
    End Function

    'DataGrid内のセルでタブキーを押された時に、ほかのコントロールにフォーカスを移動します
    'When a tab key is pushed in the cell in DataGrid, a focus is moved to other control.
    Const WM_KEYDOWN As Integer = &H100
    Private mControl As Windows.Forms.Control = Me

    Public Property NextControl() As Control
        Get
            Return mControl
        End Get
        Set(ByVal Value As Control)
            mControl = Value
        End Set
    End Property

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, _
    ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If msg.Msg = WM_KEYDOWN Then
            If (keyData = Keys.Tab) Then
                mControl.Focus()
                Return (True)
            Else
                Return MyBase.ProcessCmdKey(msg, keyData)
            End If
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

    Private Sub CustomDataGrid_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        'ヘッダーに線を引くことができないため自分で描画します。
        If (headerLine) Then
            Dim height As Integer = Me.Font.Height + 5
            Dim width As Integer = 0 - getHScroll().Value
            Dim i As Integer = 0

            For i = 0 To Me.Controls.Count - 3
                width += Me.TableStyles.Item(0).GridColumnStyles(i).Width
                '縦線ラインを引きます
                e.Graphics.DrawLine(New System.Drawing.Pen(Me.TableStyles.Item(0).GridLineColor), _
               New System.Drawing.PointF(width - 1, 0), New System.Drawing.PointF(width - 1, height))
            Next i
            '横線ラインを引きます
            e.Graphics.DrawLine(New System.Drawing.Pen(Me.TableStyles.Item(0).GridLineColor), _
            New System.Drawing.PointF(0, height), New System.Drawing.PointF(width - 1, height))
        End If
    End Sub

    Private Sub CustomDataGrid_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Scroll
        Me.Refresh()
    End Sub

    Protected Overrides Sub OnMouseWheel(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseWheel(e)
        Me.Focus()
    End Sub
End Class


Public Class ColorDataGridTextBoxColumn
    Inherits DataGridTextBoxColumn
    'Paintメソッドをオーバーライドする
    Protected Overloads Overrides Sub Paint( _
            ByVal g As Graphics, _
            ByVal bounds As Rectangle, _
            ByVal source As CurrencyManager, _
            ByVal rowNum As Integer, _
            ByVal backBrush As Brush, _
            ByVal foreBrush As Brush, _
            ByVal alignToRight As Boolean _
            )
        If Not (Me.DataGridTableStyle.DataGrid.CurrentCell.RowNumber = rowNum) Then

            foreBrush = New SolidBrush(Color.Black)
            backBrush = New SolidBrush(System.Windows.Forms.Form.DefaultBackColor)

        End If

        MyBase.Paint(g, bounds, source, rowNum, _
            backBrush, foreBrush, alignToRight)
    End Sub
End Class



▲トップページ