Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2016. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: DataGridView with Two Header Rows
- '
- ' Author: Joseph L. Bolen
- ' Date Created: 01 APR 2016
- '
- ' Description: Demo of how to create a group header row in a DataGridView.
- '
- ' Documentation is at:
- ' App's screen image is at: http://imgur.com/vOTIgSg
- ' Visual Basic .Net Source Code - http://pastebin.com/gL3ZYchB
- '-------------------------------------------------------------------------------------------
- Imports System.Data.SqlClient
- Imports System.Configuration
- Public Class EmployeeForm
- Private Sub EmployeeForm_Load(sender As Object, e As EventArgs) _
- Handles Me.Load
- With EmployeeDGV
- .AlternatingRowsDefaultCellStyle.BackColor = Color.WhiteSmoke
- .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
- .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomLeft
- .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
- .ColumnHeadersHeight *= 2
- .ColumnHeadersDefaultCellStyle.Font = New Font(.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold)
- End With
- LoadDGV()
- End Sub
- Private Sub LoadDGV()
- Dim bs As New BindingSource
- Dim conn As String = ConfigurationManager.ConnectionStrings("Northwind").ConnectionString
- Dim query As String = “SELECT FirstName, LastName, Title, Address, City, Region, PostalCode " &
- "FROM Employees " &
- "ORDER BY LastName, FirstName;”
- Try
- Using con As New SqlConnection(conn)
- Using cmd As New SqlCommand(query, con)
- con.Open()
- Using rdr As SqlDataReader = cmd.ExecuteReader()
- If rdr.HasRows Then
- bs.DataSource = rdr
- EmployeeDGV.DataSource = bs
- Else
- MessageBox.Show("Search criteria yielded no records.",
- Me.Text,
- MessageBoxButtons.OK,
- MessageBoxIcon.Information)
- End If
- End Using
- End Using
- End Using
- Catch ex As Exception
- MessageBox.Show(ex.Message,
- Me.Text,
- MessageBoxButtons.OK,
- MessageBoxIcon.Error)
- End Try
- End Sub
- Private Sub EmployeeDGV_Paint(sender As Object, e As PaintEventArgs) _
- Handles EmployeeDGV.Paint
- Dim myFormat As New StringFormat()
- myFormat.Alignment = StringAlignment.Center
- myFormat.LineAlignment = StringAlignment.Center
- ' Header Group One
- Dim HdrText As String = "Employee Detail"
- Dim HdrRect As Rectangle = EmployeeDGV.GetCellDisplayRectangle(0, -1, True)
- Dim HdrWidth As Integer = 0
- For i As Integer = 0 To 2 ' First three columns
- HdrWidth += EmployeeDGV.GetCellDisplayRectangle(i, -1, True).Width
- Next
- HdrRect = New Rectangle(HdrRect.X, HdrRect.Y, HdrWidth, HdrRect.Height \ 2)
- e.Graphics.FillRectangle(New SolidBrush(Color.LightBlue), HdrRect)
- e.Graphics.DrawString(HdrText, EmployeeDGV.ColumnHeadersDefaultCellStyle.Font,
- New SolidBrush(EmployeeDGV.ColumnHeadersDefaultCellStyle.ForeColor), HdrRect, myFormat)
- ' Next Header Group / Last Four Columns
- HdrText = "Address Information"
- HdrRect = EmployeeDGV.GetCellDisplayRectangle(3, -1, True)
- HdrWidth = 0
- For i As Integer = 3 To EmployeeDGV.ColumnCount - 1 ' Last four columns
- HdrWidth += EmployeeDGV.GetCellDisplayRectangle(i, -1, True).Width
- Next
- HdrRect = New Rectangle(HdrRect.X, HdrRect.Y, HdrWidth, HdrRect.Height \ 2)
- e.Graphics.FillRectangle(New SolidBrush(Color.Lavender), HdrRect)
- e.Graphics.DrawString(HdrText, EmployeeDGV.ColumnHeadersDefaultCellStyle.Font,
- New SolidBrush(EmployeeDGV.ColumnHeadersDefaultCellStyle.ForeColor), HdrRect, myFormat)
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment