|
■No4752 (亜希子 さん) に返信
> VB2005データグリッドビューのRowヘッダーのグレーの部分に123(1行目、2行目、3行目)と数字をつけることは可能でしょうか?
ヘッダセルには、任意のデータを渡せます。
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyClass.Load
Dim dgv As New DataGridView()
dgv.Dock = DockStyle.Fill
Controls.Add(dgv)
dgv.AllowUserToAddRows = False
dgv.RowCount = 3
dgv.ColumnCount = 3
dgv.TopLeftHeaderCell.Value = "左上"
dgv.Columns(0).HeaderCell.Value = "列0"
dgv.Columns(1).HeaderCell.Value = "列1"
dgv.Columns(2).HeaderCell.Value = "列2"
dgv.RowHeadersWidth = 70
For row As Integer = 0 To 2
dgv.Rows(row).HeaderCell.Value = "行" & CStr(row)
For col As Integer = 0 To 2
dgv(col, row).Value = String.Format("{0}-{1}", row, col)
Next
Next
End Sub
もしくは、行ごとに個別にセットするのではなく、自分で「描画」する手法もあります。
Private Sub DataGridView1_CellPainting(ByVal sender As Object, _
ByVal e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
If e.ColumnIndex = -1 AndAlso e.RowIndex >= 0 Then
e.PaintBackground(e.ClipBounds, False)
e.PaintContent(e.CellBounds)
Using brush As New SolidBrush(e.CellStyle.ForeColor), format As New StringFormat()
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
format.Trimming = StringTrimming.None
Dim cellText As String = e.RowIndex.ToString()
e.Graphics.DrawString( _
cellText, _
e.CellStyle.Font, _
brush, _
e.CellBounds, _
format)
End Using
e.Handled = True
End If
End Sub
|