Question How to set the selected item in a databound datagridviewcombobox

mattwilkinson

New member
Joined
Jul 31, 2008
Messages
2
Programming Experience
10+
I have a datagridview on which I am displaying several combo boxes. These are databound to a list of items from a DB table.
This is my code I am using to set up the combobox:
VB.NET:
Expand Collapse Copy
dgvCombo = New DataGridViewComboBoxColumn
        dgvCombo.HeaderText = "Part"
        dgvCombo.DisplayMember = "PartDescription"
        dgvCombo.ValueMember = "PartDescriptionID"
        dgvCombo.DataSource = _dvParts
        dgvCombo.MaxDropDownItems = MAXDROPDOWNITEMS
        dgvCombo.Width = 250
        dgvPaint.Columns.Add(dgvCombo)

I now want to select an item in the list based on the PartDescriptionID e.g.
VB.NET:
Expand Collapse Copy
For Each dr As DataRow In dt.Rows
     dgvPaint.Rows(nRow - 1).Cells(COLPART).Value = dr.Item("psdLinkToPartDescriptionID").ToString
next

However this doesn't select the displaymember it just sets the text of the cell to the ID.

How can I specify that I want to select one item in the bound list based on the ID in each

It would be simple if there was a selectedvalue property but one doesn't exist!
 
I dont understand. You have written a routine there that changes the value of a load of cells.. what did you think it would have to do with selecting them?

Are you saying you want to implement some kind of search by highlighting the first cell in the grid that contains the text the user typed?

Might i suggest you take a look at the .Filter property of the bindingsource the datagrid is bound to?
 
I appreciate that I am changing the cell value - what I want to do is bind to a datasource which contains the ID and the text of the data and then set the selectedvalue using the ID (as I would normally do with dropdownlist)

How can I do this?
 
Have you tried setting the value in the underlying data source (the one the combo box actually edits) to be the value you want?
 
Back
Top