1 Previous Next 

DataGrid ColumnStyles: DataGridComboBox(Display Member)


Here is another datagrid combobox example that uses a display member.
The data is stored as a number but a word is displayed.


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

   Private Sub Form1_Load(ByVal sender As System.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", 4}, True)
        dt.LoadDataRow(New Object() {"Cor Ligthert", 2}, True)
        dt.LoadDataRow(New Object() {"Terry Burns", 3}, True)
        dt.LoadDataRow(New Object() {"Armin Zignler", 1}, True)
        dt.LoadDataRow(New Object() {"Herfried K. Wagner", 0}, True)
        dt.LoadDataRow(New Object() {"Jay B Harlow", 5}, 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("New York")
        cmbTxtCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList
        DataGrid1.TableStyles.Add(ts)

    End Sub
End Class

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

    Dim strValues() As String = {"Austria", "Germany", "Netherlands", "United Kingdom", "Florida", "New York"}
    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 = GetColumnValueAtRow(source, rowNum)
        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.SelectedIndex)
        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.SelectedIndex)
            isEditing = False
            Invalidate()
        End If
        ColumnComboBox.Hide()
    End Sub

    Protected Overrides Function GetColumnValueAtRow(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Object
        Dim intValue As Integer

        intValue = CInt(MyBase.GetColumnValueAtRow(source, rowNum))
        Return strValues(intValue)
    End Function
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
End Class




1 Previous Next