By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,877 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,877 IT Pros & Developers. It's quick & easy.

Combobox in a Datagrid

P: n/a

I know there are a lot of postings out there concerning this subject
already, but so far I have not discovered anything that meets my
specific needs. Everything I have found populates the combobox column
with values from a database. I need to be able to manually set these
values in my code. I have tried to make the example code I have found
work for my particular situation but I am having a lot of trouble.
Does anyone have some advice on how to approach coding this?

Basically what I need to do is exactly what Access does - the user
specifies the list of possible values for a particular column in a
table, then Access displays this list as a combobox to the user. The
user can then select an option from the combobox and Access populates
the column/row with the specified value. And every new row contains
this combobox waiting to be selected by the user.

Any guidance would be greatly appreciated! Thanks!!

Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies

P: n/a
Hi Simchajoy.

I think that this complete sample will help you, you can open a windoform
project and just paste it in, than add 3 buttons and a datagrid on that

I hope this helps?


'\\\needs a datagrid and 3 buttons on a form
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 ds"
Me.Button2.Text = "Write ds"
Me.Button3.Text = "New ds"
End Sub
Private Sub FillGrid()
Static done As Boolean
If Not done Then
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.DropDownStyle =
done = True
End If

End Sub
'make dataset
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
Dim dt As New DataTable("Names")
For i As Integer = 1 To 6
Dim dr As DataRow = dt.NewRow
dr(0) = i.ToString
dt.Rows(0)(1) = "Herfried K. Wagner"
dt.Rows(1)(1) = "Armin Zingler"
dt.Rows(2)(1) = "Ken Tucker"
dt.Rows(3)(1) = "Terry Burns"
dt.Rows(4)(1) = "Jay B Harlow"
dt.Rows(5)(1) = "Cor Ligthert"
dt.Rows(0)(2) = "Austria(EU)"
dt.Rows(1)(2) = "Germany(EU)"
dt.Rows(2)(2) = "Georgia(US)"
dt.Rows(3)(2) = "UK(EU)"
dt.Rows(4)(2) = "New York(US)"
dt.Rows(5)(2) = "Holland(EU)"
End Sub
'Read dataset
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
End If
End Sub
'write dataset
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
End Class
' The simple DatagridCombobox
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
Nov 20 '05 #2

P: n/a

Thank you! That helps a lot!

*** Sent via Devdex ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #3

P: n/a


This code you posted has worked great for me. However, now I have
another question that maybe you can help me with.

I have many DataGridComboBoxColumns in my datagrid and I want to execute
different code in each column when the user commits a change to the
combobox. Right now the "ComboStartEditing" sub handles the
"SelectionChangeCommitted" event for any combobox from any

I don't want to change this but I want to place my extra code for
handling the combobox.SelectionChangeCommitted event in my class that
builds the DataGridTableStyle.GridColumnStyles. Is this possible?? Is
it even possible to access the combobox.SelectionChangeCommitted event
from outside the DataGridComboBoxColumn instance?

Thanks for all the help!!


*** Sent via Developersdex ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #4

P: n/a
Hi Joy,

I think that this will fits what you need

AddHandler CType(cmbTextColumn.ColumnComboBox, _
ComboBox).SelectionChangeCommitted, AddressOf mychange
Private Sub mychange(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show("I was changed")
End Sub

I hope this helps?

Nov 20 '05 #5

P: n/a

i need a datagridcombobox component
*** Sent via Developersdex ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #6

P: n/a

When you have no datarelations than this sample will maybe help (the class
is at the bottom)
When you have datarelations than this sample

You can try both, however check yourself of course.

I hope this helps?

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.