datagrid with a combobox



OK. So I've been to and learned a lot about what I might do with a datagrid. But I'm STILL not able to do what I want to do. I want to be able be able to enter data into my grid cell using a combobox. I want the combobox to display items based on my selected DisplayMember and update the datasource based on my selected ValueMember. So far, so good. But when the user leaves the combobox cell, I want the value of the DisplayMember to be displayed, not the ValueMember. The closest I've come to this is to Paint all the cells in the column with the same (most recently selected) DisplayMember value

While I'm at it, I'd like to derive an inherited ComboBox that has a ValueMember, a DisplayMember, and a COLLECTION of DisplayColumns in the DropDown list. Any help



Cor Ligthert

Hi Pat,

Did you try this sample that I have made with as base the comboboxsample on
Syncfusion (which I modified). The modifed combobox class is at the end.

I hope this helps?


'\\\needs a datagrid and 2 buttons on a form
'Used for the comboboxcolumn is a modified sample from
'To start do the Read/Create ds and cancel direct,
' than a start dataset will be created
Dim ds As New DataSet("Test")
Private Sub Form1_Load(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles MyBase.Load
Me.Button1.Text = "Read/Create ds"
Me.Button2.Text = "Write ds"
End Sub
Private Sub FillGrid()
Dim dv As New DataView(ds.Tables(0))
dv.AllowNew = False
DataGrid1.DataSource = dv
Dim ts As New DataGridTableStyle
ts.MappingName = "Names"
Dim textCol As New DataGridTextBoxColumn
textCol.MappingName = "IdName"
textCol.HeaderText = "Id"
textCol.Width = 20
textCol = New DataGridTextBoxColumn
textCol.MappingName = "Name"
textCol.HeaderText = "Name"
textCol.Width = 120
Dim cmbTxtCol As New DataGridComboBoxColumn
cmbTxtCol.MappingName = "Country"
cmbTxtCol.HeaderText = "Countries"
cmbTxtCol.Width = 100
ts.PreferredRowHeight = (cmbTxtCol.ColumnComboBox.Height + 3)
cmbTxtCol.ColumnComboBox.DataSource = ds.Tables(1)
cmbTxtCol.ColumnComboBox.DisplayMember = "Country"
cmbTxtCol.ColumnComboBox.ValueMember = "IdCountry"
cmbTxtCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim of As New SaveFileDialog
If of.ShowDialog = DialogResult.OK Then
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim fo As New OpenFileDialog
If fo.ShowDialog() = DialogResult.OK Then
Dim dtName As New DataTable("Names")
Dim dcIdName As New DataColumn("IdName")
Dim dcName As New DataColumn("Name")
Dim dcCountryN As New DataColumn("Country")
For i As Integer = 1 To 6
Dim dr As DataRow = dtName.NewRow
dr(0) = i.ToString
dtName.Rows(0)(1) = "Herfried K. Wagner"
dtName.Rows(1)(1) = "Armin Zingler"
dtName.Rows(2)(1) = "Ken Tucker"
dtName.Rows(3)(1) = "CJ Taylor"
dtName.Rows(4)(1) = "Jay B Harlow"
dtName.Rows(5)(1) = "Cor Ligthert"
dtName.Rows(0)(2) = "Austria(EU)"
dtName.Rows(1)(2) = "Germany(EU)"
dtName.Rows(2)(2) = "Georgia(US)"
dtName.Rows(3)(2) = "Illinois(US)"
dtName.Rows(4)(2) = "New York(US)"
dtName.Rows(5)(2) = "Holland(EU)"
Dim dtCountry As New DataTable("Countries")
Dim dcIdCountry As New DataColumn("IDCountry")
Dim dcCountry As New DataColumn("Country")
For i As Integer = 1 To 6
Dim dr As DataRow = dtCountry.NewRow
dr(0) = i.ToString
dtCountry.Rows(0)(1) = "Austria(EU)"
dtCountry.Rows(1)(1) = "Germany(EU)"
dtCountry.Rows(2)(1) = "Holland(EU)"
dtCountry.Rows(3)(1) = "Georgia(US)"
dtCountry.Rows(4)(1) = "Illinois(US)"
dtCountry.Rows(5)(1) = "New York(US)"
End If
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
Shared Sub New()
End Sub
Public Sub 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,
mRowNum = rowNum
cmSource = source
ColumnComboBox.Parent = Me.TextBox.Parent
ColumnComboBox.Location = Me.TextBox.Location
ColumnComboBox.Size = New Size(Me.TextBox.Size.Width,
ColumnComboBox.Text = Me.TextBox.Text
TextBox.Visible = False
ColumnComboBox.Visible = True
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
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
End If
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
End If
End Sub


Thanks for the response

This looks very similar to the syncfusion implementation. I have not actually tried to run yours, but I still don't see how it will work as I want. Specifically, I want to DISPLAY the ComboBox's DisplayMember value in the datagrid, but STORE the ComboBox's ValueMember in the DataGrid's datasource. It appears that your example (and all others that I've looked at), either store AND display the DisplayMember, or store AND display the ValueMember. Here is a listing of what I'm trying to do by intercepting the Paint method. It doesn't actually work, but I THINK I'm on the right track. In particular, have a look at the GetColumnTextAtRow function. Any advice

Public Class DataGridComboBoxColum
Inherits DataGridColumnStyl

' UI constants

Private xMargin As Integer =
Private yMargin As Integer =
Private WithEvents Combo As DataGridComboBo
Private WithEvents tb As TextBo
Private _DisplayMember As Strin
Private _ValueMember As Strin
Private _rowNum As Intege
Private WithEvents _source As CurrencyManage

' Used to track editing stat

Private OldVal As String = String.Empt
Private InEdit As Boolean = Fals

Public Property ComboBox() As DataGridComboBo
Return Comb
End Ge
Set(ByVal Value As DataGridComboBox
Combo = Valu
End Se
End Propert

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
Dim Text As String = GetText(GetColumnTextAtRow(Source, RowNum)
PaintText(g, Bounds, Text, AlignToRight
End Su

Private Function GetText(ByVal Value As Object) As Strin
If Not Value Is Nothing The
Debug.WriteLine("GetText(" & Value.ToString & ")"
Debug.WriteLine("GetText(Value is Nothing)"
End I
If Value Is System.DBNull.Value Then Return NullTex

If Not Value Is Nothing The
Return Value.ToStrin
Debug.WriteLine("Value is Nothing"
Return String.Empt
End I

End Functio

Private Sub PaintText(ByVal g As Graphics,
ByVal Bounds As Rectangle,
ByVal Text As String,
ByVal AlignToRight As Boolean

Dim BackBrush As Brush = New SolidBrush(Me.DataGridTableStyle.BackColor
Dim ForeBrush As Brush = New SolidBrush(Me.DataGridTableStyle.ForeColor
PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight
End Su

Private Function GetColumnTextAtRow(ByVal Source As CurrencyManager, ByVal RowNum As Integer) As Objec
Dim value As Object = Me.GetColumnValueAtRow(Source, RowNum
Dim dSource As Objec
Dim dr As DataRo

dSource = Combo.DataSourc
If value Is System.DBNull.Value The
Return NullTex
End I
If Not dSource Is Nothing The
If TypeOf dSource Is DataTable The
dSource = CType(dSource, DataTable
If Not dSource.Columns.Contains(Combo.DisplayMember)
OrElse Not dSource.Columns.Contains(Combo.ValueMember) The
Return NullTex
End I
For Each dr In dSource.Row
If value = dr(Combo.ValueMember) The
Return dr(Combo.DisplayMember)
End If
Return NullText
ElseIf TypeOf dSource Is DataView Then
dSource = CType(dSource, DataView)
If Not dSource.Table.Columns.Contains(Combo.DisplayMember) _
OrElse Not dSource.Table.Columns.Contains(Combo.ValueMember) Then
Return value
End If
For Each dr In dSource.Table.Rows
If value = dr(Combo.ValueMember) Then
Return dr(Combo.DisplayMember)
End If
Return NullText
End If
End If
End Function

Private Sub Combo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Combo.SelectedIndexChanged
tb.Text = CType(sender, ComboBox).Text
End Sub

Public Class DataGridComboBox
Inherits ComboBox
Public Sub New()
End Sub

Private _modified As Boolean = False

Public isInEditOrNavigateMode As Boolean = True

Public Property Modified() As Boolean
Return _modified
End Get
Set(ByVal Value As Boolean)
_modified = Value
End Set
End Property

End Class

End Class

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question