2010/06/21(Mon) 22:20:21 編集(投稿者)
初心者さんこんにちは。
例えば,下のようにすると特定の「行」に罫線を引くことができます。
ただ,下の例は「動けばよい」という程度のもので,エラー処理などがありませんので,そのまま使ったりはしないでください。
Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If e.RowIndex > -1 And e.ColumnIndex > -1 Then
Select Case Me.DataGridView1.Rows(e.RowIndex).Cells(0).Value
Case "バナナ", "たまねぎ"
Using brush As New SolidBrush(e.CellStyle.BackColor)
e.Graphics.FillRectangle(brush, e.CellBounds)
End Using
Using pen As New Pen(Me.DataGridView1.GridColor)
With e.CellBounds
.Offset(-1, -1)
e.Graphics.DrawRectangle(pen, .Left, .Top, .Width, .Height)
End With
End Using
Using Pen As New Pen(Brushes.Black, 2)
With e.CellBounds
.Offset(-1, -1)
e.Graphics.DrawLine(Pen, .Left, .Bottom, .Right, .Bottom)
End With
End Using
If e.Value IsNot Nothing Then
Using brush As New SolidBrush(e.CellStyle.ForeColor)
Dim y As Integer = e.CellBounds.Y + (e.CellBounds.Height - e.Graphics.MeasureString(e.Value.ToString, e.CellStyle.Font).Height) / 2
e.Graphics.DrawString(e.Value.ToString, e.CellStyle.Font, brush, e.CellBounds.X, y, StringFormat.GenericDefault)
End Using
End If
e.Handled = True
End Select
End If
End Sub