Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I add text to the row header, e.g., a row number, in a DataGridView?

You can set a row's HeaderCell.Value property to a value and it will be painted. Alternatively - and fairly easy - handle the RowPostPaint and paint the row number in the header cell like so:

Private Sub dataGridView1_RowPostPaint(ByVal sender As Object, _
  ByVal e As DataGridViewRowPostPaintEventArgs) _
    Handles dataGridView1.RowPostPaint
  ' Paint the row number on the row header.
  ' The using statement automatically disposes the brush.
  Using b As SolidBrush = _
    New SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor)
  e.Graphics.DrawString( _
    e.RowIndex.ToString(System.Globalization.CultureInfo.CurrentUICulture), _
    e.CellStyle.Font, _
    b, _
    e.RowBounds.Location.X + 20, _
    e.RowBounds.Location.Y + 4)
End Using
End Sub