Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel
Public Class DataGridButtonColumn
Inherits DataGridColumnStyle
Public Event ButtonClick As EventHandler
' Button to be embedded in the cell
Private m_button As New Button
Private Const BUTTON_SIZE = 23
Public Sub New()
' Hide the button
m_button.Visible = False
m_button.Text = "..."
m_button.BackColor = Color.FromKnownColor(KnownColor.Control)
' Add the Event Handlet to monitor Button Clicks
AddHandler m_button.Click, AddressOf OnButtonClick
End Sub
Protected Overrides Sub Abort(ByVal rowNum As Integer)
' Remove the button and redraw the cell
Invalidate()
End Sub
Protected Overrides Function Commit(ByVal dataSource As CurrencyManager, ByVal rowNum As Integer) As Boolean
' Edit is finishing...
' Redraw the cell
Invalidate()
Return True
End Function
Protected Overloads Overrides Sub Edit(ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
' Initiate an Edit..
' If the cell is visible, setup the Button
If cellIsVisible Then
' Position and Size the Button
m_button.Location = New Point(bounds.X + 2, bounds.Y + 2)
m_button.Size = New Size(bounds.Width - 4, bounds.Height - 4)
' Make it visible
m_button.Visible = True
Else
' Hide the Button
m_button.Visible = False
End If
' If the Button is visible, redraw the cell to show it
If m_button.Visible Then
DataGridTableStyle.DataGrid.Invalidate(bounds)
End If
End Sub
' Override the "Width" property to allow us to
' resize the Button to fit the column
Public Overrides Property Width() As Integer
Get
Return MyBase.Width
End Get
Set(ByVal Value As Integer)
MyBase.Width = Value
m_button.Width = Value - 4
End Set
End Property
Protected Overrides Function GetPreferredSize(ByVal g As Graphics, ByVal value As Object) As Size
' Return the default size the cell should be
Return New Size(BUTTON_SIZE, BUTTON_SIZE)
End Function
Protected Overrides Function GetMinimumHeight() As Integer
' Return the minimum height this column can be
Return BUTTON_SIZE
End Function
Protected Overrides Function GetPreferredHeight(ByVal g As Graphics, ByVal value As Object) As Integer
' Return the default/preferred height of this column
Return BUTTON_SIZE
End Function
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer)
' Call the overload
Paint(g, bounds, [source], rowNum, False)
End Sub
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByVal alignToRight As Boolean)
' Call the overload
Paint(g, bounds, [source], rowNum, Brushes.White, Brushes.Black, alignToRight)
End Sub
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
' Draw the cell to display the value
' Get the current cell value
Dim value As Object = GetColumnValueAtRow([source], rowNum)
Dim rect As Rectangle = bounds
' Clear the cell
g.FillRectangle(backBrush, rect)
End Sub
Protected Overrides Sub SetDataGridInColumn(ByVal value As DataGrid)
' Assign the Button to the specified Grid
MyBase.SetDataGridInColumn(value)
If Not (m_button.Parent Is Nothing) Then
m_button.Parent.Controls.Remove(m_button)
End If
If Not (value Is Nothing) Then
value.Controls.Add(m_button)
End If
End Sub
Private Sub OnButtonClick(ByVal sender As Object, ByVal e As EventArgs)
' Event handler for the Button
MyBase.ColumnStartedEditing(m_button)
RaiseEvent ButtonClick(Me, EventArgs.Empty)
End Sub
End Class