Visual Basic 2005-如何在 DataGridView 控制項的資料列標題上繪製編號

摘要:Visual Basic 2005-如何在 DataGridView 控制項的資料列標題上繪製編號

圖表1 

有許多朋友詢問我,如何在DataGridView控制項的資料列標題上繪製出額外的數字編號。欲完成這樣的處理非常簡單,您只需替DataGridViewRow控制項的RowPostPaint事件處理常式撰寫下列程式碼即可: 

Private Sub dataGridView1_RowPostPaint(ByVal sender As Object, _
  ByVal e As DataGridViewRowPostPaintEventArgs) _
  Handles DataGridView1.RowPostPaint

  Using b As SolidBrush = _
      New SolidBrush(DataGridView1.RowHeadersDefaultCellStyle.ForeColor)

      e.Graphics.DrawString(e.RowIndex.ToString( _
      System.Globalization.CultureInfo.CurrentUICulture), _
                     DataGridView1.DefaultCellStyle.Font, _
                                                       b, _
                             e.RowBounds.Location.X + 20, _
                             e.RowBounds.Location.Y + 4)

  End Using
End
Sub