This has caught me out a couple of times now so it’s definitely worth mentioning, if only for my own memory!

Here is the source code to add a databound combo box column to a datagridview. The line of code which is important here, and is the one that always catches me out is the DataPropertyName property of the combo box. This property does not exist in a standalone combo box so it’s very easy to leave it out. This combo box is databound to a dataset.

Dim cmb As New DataGridViewComboBoxColumn()
cmb.HeaderText = “Column header
cmb.Name = “Combo box name
cmb.DataSource = dataset.Tables(0)
cmb.DataPropertyName = “Field to bind to
cmb.DisplayMember = “Field to show to user
cmb.ValueMember = “Value of field

And there you have it. You can now access the databound value of the combox box within the datagridview by simply parsing the dataset the datagridview is bound to. For example:

Dim dr As DataRow
For Each dr In ds.Tables(0).Rows
MsgBox(dr.Item(Combo box name).ToString)

Leave a reply

Your email address will not be published.