1 Previous Next 

DataGrid ColumnStyles: DataGridComboBox(Simple)


This is a (changed and optimized) version from the one which we saw in past made by a lot of persons for the Windows Forms DataGrid which has no standard combobox, this one works with Option Strict On.

At the end of the sample is showed how to use it

Be aware this is a Tip for a WindowsForms DataGrid not a DataGridView

Public Class DataGridComboBoxColumn
    Inherits DataGridTextBoxColumn
    Public WithEvents ColumnComboBox As NoKeyUpCombo 'special class
    Private WithEvents cmSource As CurrencyManager
    Private mRowNum As Integer
    Private isEditing As Boolean
    Shared Sub New()
    End Sub
    Public Sub New()
        MyBase.New()
        ColumnComboBox = New NoKeyUpCombo
        AddHandler ColumnComboBox.SelectionChangeCommitted, _
        New EventHandler(AddressOf ComboStartEditing)
    End Sub
    Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager, _
    ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal readOnly1 As Boolean, _
    ByVal instantText As String, ByVal cellIsVisible As Boolean)
        MyBase.Edit(source, rowNum, bounds, readOnly1, instantText, cellIsVisible)
        mRowNum = rowNum
        cmSource = source
        ColumnComboBox.Parent = Me.TextBox.Parent
        ColumnComboBox.Location = Me.TextBox.Location
        ColumnComboBox.Size = New Size(Me.TextBox.Size.Width, ColumnComboBox.Size.Height)
        ColumnComboBox.Text = Me.TextBox.Text
        TextBox.Visible = False
        ColumnComboBox.Visible = True
        ColumnComboBox.BringToFront()
        ColumnComboBox.Focus()
    End Sub
    Protected Overloads Overrides Function Commit(ByVal dataSource As _
    CurrencyManager, ByVal rowNum As Integer) As Boolean
        If isEditing Then
            isEditing = False
            SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text)
        End If
        Return True
    End Function
    Private Sub ComboStartEditing(ByVal sender As Object, ByVal e As EventArgs)
        isEditing = True
        MyBase.ColumnStartedEditing(DirectCast(sender, Control))
    End Sub
    Private Sub LeaveComboBox(ByVal sender As Object, ByVal e As EventArgs) _
    Handles ColumnComboBox.Leave
        If isEditing Then
            SetColumnValueAtRow(cmSource, mRowNum, ColumnComboBox.Text)
            isEditing = False
            Invalidate()
        End If
        ColumnComboBox.Hide()
    End Sub
End Class
Public Class NoKeyUpCombo
    Inherits ComboBox
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg <> &H101 Then
            MyBase.WndProc(m)
        End If
End Sub
///


This is a sample how to use it
\\\Sample to use the simple datagrid combobox it needs a form with a datagrid.
  Private Sub Form1_Load(ByVal sender As Object, ByVal e _
    As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable("Names")
        dt.Columns.Add("Name")
        dt.Columns.Add("State")
        dt.LoadDataRow(New Object() {"Ken Tucker", "Florida"}, True)
        dt.LoadDataRow(New Object() {"Cor Ligthert", "Netherlands"}, True)
        dt.LoadDataRow(New Object() {"Terry Burns", "United Kingdom"}, True)
        dt.LoadDataRow(New Object() {"Armin Zignler", "Germany"}, True)
        dt.LoadDataRow(New Object() {"Herfried K. Wagner", "Austria"}, True)
        dt.LoadDataRow(New Object() {"Jay B Harlow", "New York"}, True)

'above only to build a sample datatable
        dt.DefaultView.AllowNew = False
        DataGrid1.DataSource = dt.DefaultView
        Dim ts As New DataGridTableStyle
        ts.MappingName = "Names"
        Dim textCol As New DataGridTextBoxColumn
        textCol.MappingName = "Name"
        textCol.HeaderText = "Name"
        textCol.Width = 120
        ts.GridColumnStyles.Add(textCol)
        Dim cmbTxtCol As New DataGridComboBoxColumn
        cmbTxtCol.MappingName = "State"
        cmbTxtCol.HeaderText = "States"
        cmbTxtCol.Width = 100
        ts.GridColumnStyles.Add(cmbTxtCol)
        ts.PreferredRowHeight = (cmbTxtCol.ColumnComboBox.Height + 3)
        cmbTxtCol.ColumnComboBox.Items.Add("Austria")
        cmbTxtCol.ColumnComboBox.Items.Add("Germany")
        cmbTxtCol.ColumnComboBox.Items.Add("Netherlands")
        cmbTxtCol.ColumnComboBox.Items.Add("United Kingdom")
        cmbTxtCol.ColumnComboBox.Items.Add("Florida")
        cmbTxtCol.ColumnComboBox.Items.Add("NewYork")
        cmbTxtCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList
        DataGrid1.TableStyles.Add(ts)
    End Sub



1 Previous Next