
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel

' This example shows how to create your own column style that
' hosts a control, in this case, a DateTimePicker.
Public Class ComboColumn
    Inherits DataGridColumnStyle
    Private comboPicker As New ComboBox
    ' The isEditing field tracks whether or not the user is
    ' editing data with the hosted control.
    Private isEditing As Boolean

    Public Sub New(ByVal allValues As String(), ByVal startValue As String)
        comboPicker.Visible = False
        Dim counter As Integer
        Try

            For counter = 0 To allValues.Length - 1
                comboPicker.Items.Add(allValues(counter))
                If allValues(counter) = startValue Then
                    comboPicker.SelectedIndex = counter
                End If
            Next
        Catch ex As Exception
            Console.WriteLine(ex)
        End Try

    End Sub

    Protected Overrides Sub Abort(ByVal rowNum As Integer)
        isEditing = False
        RemoveHandler comboPicker.SelectedIndexChanged, _
        AddressOf comboPickerValueChanged
        Invalidate()
    End Sub

    Protected Overrides Function Commit _
    (ByVal dataSource As CurrencyManager, ByVal rowNum As Integer) _
    As Boolean
        comboPicker.Bounds = Rectangle.Empty

        AddHandler comboPicker.SelectedIndexChanged, _
        AddressOf comboPickerValueChanged

        If Not isEditing Then
            Return True
        End If
        isEditing = False

        Try
            Dim value As String = comboPicker.Text
            SetColumnValueAtRow(dataSource, rowNum, value)
        Catch
        End Try

        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)
        Dim value As String
        Try
            value = CType(GetColumnValueAtRow([source], rowNum), String)

        Catch ex As Exception
            value = ""
        End Try
        If cellIsVisible Then
            comboPicker.Bounds = New Rectangle _
            (bounds.X + 2, bounds.Y + 2, bounds.Width - 4, _
            bounds.Height - 4)

            comboPicker.Text = value
            comboPicker.Visible = True
            AddHandler comboPicker.SelectedIndexChanged, _
            AddressOf comboPickerValueChanged
        Else
            comboPicker.Text = value
            comboPicker.Visible = False
        End If

        If comboPicker.Visible Then
            DataGridTableStyle.DataGrid.Invalidate(bounds)
        End If
    End Sub

    Protected Overrides Function GetPreferredSize( _
    ByVal g As Graphics, _
    ByVal value As Object) As Size
        Return New Size(100, comboPicker.PreferredHeight + 4)
    End Function

    Protected Overrides Function GetMinimumHeight() As Integer
        Return comboPicker.PreferredHeight + 4
    End Function


    Protected Overrides Function GetPreferredHeight(ByVal g As Graphics, ByVal value As Object) As Integer
        Return comboPicker.PreferredHeight + 4
    End Function


    Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal [source] As CurrencyManager, ByVal rowNum As Integer)
        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)
        Paint(g, bounds, [source], rowNum, Brushes.Red, Brushes.Blue, 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)
        Dim [combo] As String
        Try
            [combo] = CType(GetColumnValueAtRow([source], rowNum), String)
        Catch ex As Exception
            [combo] = ""
        End Try
        Dim rect As Rectangle = bounds
        g.FillRectangle(backBrush, rect)
        rect.Offset(0, 2)
        rect.Height -= 2
        g.DrawString([combo], Me.DataGridTableStyle.DataGrid.Font, foreBrush, RectangleF.FromLTRB(rect.X, rect.Y, rect.Right, rect.Bottom))
    End Sub

    Protected Overrides Sub SetDataGridInColumn(ByVal value As DataGrid)
        MyBase.SetDataGridInColumn(value)
        If Not (comboPicker.Parent Is Nothing) Then
            comboPicker.Parent.Controls.Remove(comboPicker)
        End If
        If Not (value Is Nothing) Then
            value.Controls.Add(comboPicker)
        End If
    End Sub

    Private Sub comboPickerValueChanged(ByVal sender As Object, ByVal e As EventArgs)
        Me.isEditing = True
        MyBase.ColumnStartedEditing(comboPicker)
    End Sub


End Class
