Question DataGridViewComboBoxColumn valuemember

ihabassi

New member
Joined
May 2, 2015
Messages
2
Programming Experience
1-3
Hi,

I'm using a databound DataGridViewComboBoxColumn and I'd like to have a displaymember with is different from the value.
The solution I've thought about is:

VB.NET:
    Public Shared Sub OnComboBoxPopulate(ByVal p_tableName As String, ByVal p_MemberName As String, _
    ByVal p_ds As DataSet, Optional ByVal p_col As DataGridViewComboBoxColumn = Nothing)


        Dim dt As DataTable = New DataTable
        dt = p_ds.Tables(p_tableName)


        With p_col
            If dt IsNot Nothing Then
                '.Name = p_col.Name
                .DataSource = dt
                .ValueMember = "id"
                .DisplayMember = p_MemberName
                .DataPropertyName = "id"
            End If
        End With


    End Sub

The problem is that when I'm using only displaymember looks like everything is OK, but when I'm adding the value member to the column I get the error message:

"datagridviewcomboboxcell value is not valid" immediately when I'm leaving the cell on the datagridview.

Any help will be appreciated.

Thanks
Ihab
 
When you use a combo box column bound that way, the Value in each cell is assigned to the SelectedValue of the embedded ComboBox to tell it which item to select. In your case, the `id` in one of the cells is not equal to one of the values in the `id` column of that DataTable that you bound to the combo box column.

I'm not sure whether this will solve the problem in your case but you should ALWAYS set the DataSource last, after setting DisplayMember and/or ValueMember.

The DataPropertyName should even be being set in that method at all. It's got nothing to do with the combo box. That should be being set when you bind the grid.
 
Back
Top