JayBeeOH

DataGridViiw with Two Header Rows

Apr 1st, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.08 KB | None | 0 0
  1. '------------------------------------------------------------------------------------------
  2. '           Notice of My Copyright and Intellectual Property Rights
  3. '
  4. ' Any intellectual property contained within the program by Joseph L. Bolen remains the
  5. ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
  6. ' publish or provide such intellectual property to any other person or entity for any
  7. ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
  8. '
  9. '                 Copyright © 2016. All rights reserved.
  10. '        All trademarks remain the property of their respective owners.
  11. '-------------------------------------------------------------------------------------------
  12. ' Program Name:   DataGridView with Two Header Rows
  13. '
  14. ' Author:         Joseph L. Bolen
  15. ' Date Created:   01 APR 2016
  16. '
  17. ' Description:    Demo of how to create a group header row in a DataGridView.
  18. '
  19. '                 Documentation is at:
  20. '                   App's screen image is at: http://imgur.com/vOTIgSg
  21. '                   Visual Basic .Net Source Code - http://pastebin.com/gL3ZYchB
  22. '-------------------------------------------------------------------------------------------
  23.  
  24. Imports System.Data.SqlClient
  25. Imports System.Configuration
  26.  
  27. Public Class EmployeeForm
  28.  
  29.     Private Sub EmployeeForm_Load(sender As Object, e As EventArgs) _
  30.         Handles Me.Load
  31.  
  32.         With EmployeeDGV
  33.             .AlternatingRowsDefaultCellStyle.BackColor = Color.WhiteSmoke
  34.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
  35.             .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomLeft
  36.             .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
  37.             .ColumnHeadersHeight *= 2
  38.            .ColumnHeadersDefaultCellStyle.Font = New Font(.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold)
  39.         End With
  40.  
  41.         LoadDGV()
  42.  
  43.     End Sub
  44.  
  45.     Private Sub LoadDGV()
  46.  
  47.         Dim bs As New BindingSource
  48.         Dim conn As String = ConfigurationManager.ConnectionStrings("Northwind").ConnectionString
  49.         Dim query As String =SELECT FirstName, LastName, Title, Address, City, Region, PostalCode " &
  50.                              "FROM Employees " &
  51.                              "ORDER BY LastName, FirstName;”
  52.  
  53.         Try
  54.             Using con As New SqlConnection(conn)
  55.                 Using cmd As New SqlCommand(query, con)
  56.  
  57.                     con.Open()
  58.                     Using rdr As SqlDataReader = cmd.ExecuteReader()
  59.                         If rdr.HasRows Then
  60.                             bs.DataSource = rdr
  61.                             EmployeeDGV.DataSource = bs
  62.                         Else
  63.                             MessageBox.Show("Search criteria yielded no records.",
  64.                                             Me.Text,
  65.                                             MessageBoxButtons.OK,
  66.                                             MessageBoxIcon.Information)
  67.                         End If
  68.                     End Using
  69.                 End Using
  70.             End Using
  71.         Catch ex As Exception
  72.             MessageBox.Show(ex.Message,
  73.                             Me.Text,
  74.                             MessageBoxButtons.OK,
  75.                             MessageBoxIcon.Error)
  76.         End Try
  77.  
  78.     End Sub
  79.  
  80.     Private Sub EmployeeDGV_Paint(sender As Object, e As PaintEventArgs) _
  81.         Handles EmployeeDGV.Paint
  82.  
  83.         Dim myFormat As New StringFormat()
  84.         myFormat.Alignment = StringAlignment.Center
  85.         myFormat.LineAlignment = StringAlignment.Center
  86.  
  87.         ' Header Group One
  88.         Dim HdrText As String = "Employee Detail"
  89.         Dim HdrRect As Rectangle = EmployeeDGV.GetCellDisplayRectangle(0, -1, True)
  90.         Dim HdrWidth As Integer = 0
  91.         For i As Integer = 0 To 2 ' First three columns
  92.             HdrWidth += EmployeeDGV.GetCellDisplayRectangle(i, -1, True).Width
  93.         Next
  94.         HdrRect = New Rectangle(HdrRect.X, HdrRect.Y, HdrWidth, HdrRect.Height \ 2)
  95.         e.Graphics.FillRectangle(New SolidBrush(Color.LightBlue), HdrRect)
  96.         e.Graphics.DrawString(HdrText, EmployeeDGV.ColumnHeadersDefaultCellStyle.Font,
  97.                               New SolidBrush(EmployeeDGV.ColumnHeadersDefaultCellStyle.ForeColor), HdrRect, myFormat)
  98.  
  99.         ' Next Header Group / Last Four Columns
  100.         HdrText = "Address Information"
  101.         HdrRect = EmployeeDGV.GetCellDisplayRectangle(3, -1, True)
  102.         HdrWidth = 0
  103.         For i As Integer = 3 To EmployeeDGV.ColumnCount - 1 ' Last four columns
  104.             HdrWidth += EmployeeDGV.GetCellDisplayRectangle(i, -1, True).Width
  105.         Next
  106.         HdrRect = New Rectangle(HdrRect.X, HdrRect.Y, HdrWidth, HdrRect.Height \ 2)
  107.         e.Graphics.FillRectangle(New SolidBrush(Color.Lavender), HdrRect)
  108.         e.Graphics.DrawString(HdrText, EmployeeDGV.ColumnHeadersDefaultCellStyle.Font,
  109.                                New SolidBrush(EmployeeDGV.ColumnHeadersDefaultCellStyle.ForeColor), HdrRect, myFormat)
  110.     End Sub
  111.  
  112. End Class
Advertisement
Add Comment
Please, Sign In to add comment