1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
Public Class Form1
Private rnd As New Random
Private oldRowIndex As Int32 = 0
Public Sub New()
InitializeComponent()
Me.DataGridView1.Dock = DockStyle.Bottom
Me.Controls.Add(Me.dataGridView1)
Me.Text = "DataGridView calendar column demo"
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SetupColumns()
AddRows()
End Sub
Private Sub SetupColumns()
Dim col1 As New DataGridViewTextBoxColumn()
col1.HeaderText = "ID"
Dim col2 As New DataGridViewTextBoxColumn()
col2.HeaderText = "Name"
'add columns
Me.DataGridView1.Columns.Add(col1)
Me.DataGridView1.Columns.Add(col2)
End Sub
Private Sub AddRows()
'add 5 rows
Me.DataGridView1.RowCount = 5
Dim row As DataGridViewRow
Dim i As Integer = 1
For Each row In Me.DataGridView1.Rows
row.Cells(0).Value = (i * 10).ToString
row.Cells(1).Value = "Item" + i.ToString
i += 1
Next row
End Sub
Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter
If oldRowIndex <> -1 Then
Me.DataGridView1.InvalidateRow(oldRowIndex)
End If
oldRowIndex = e.RowIndex
End Sub
Private Sub DataGridView1_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
If oldRowIndex <> -1 Then
Me.DataGridView1.InvalidateRow(oldRowIndex)
End If
oldRowIndex = e.RowIndex
End Sub
Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
Dim rowBounds As New Rectangle(Me.DataGridView1.RowHeadersWidth,
e.RowBounds.Top, Me.DataGridView1.Columns.GetColumnsWidth(
DataGridViewElementStates.Visible) - Me.DataGridView1.HorizontalScrollingOffset + 1, e.RowBounds.Height)
If e.RowIndex = DataGridView1.CurrentRow.Index Then
'pen .
Dim penLine As New Pen(Brushes.Red, 5.0)
Dim clip As RectangleF = rowBounds
' Draw the content that spans multiple columns.
Dim p0 As New PointF(rowBounds.X, rowBounds.Y + rowBounds.Height / 2)
Dim p1 As New PointF(rowBounds.X + rowBounds.Width, rowBounds.Y + rowBounds.Height / 2)
e.Graphics.DrawLine(penLine, p0, p1)
penLine.Dispose()
Else
'peinture par defaut de la ligne courante
e.PaintCells(rowBounds, DataGridViewPaintParts.All)
End If
End Sub
End Class |
Partager