vb.netdatagridviewdatagridviewcombobox

Why does DataGridViewComboBoxColumn display a ValueMember?


I have a small problem. DataGridViewComboBoxColumn displays a value from ValueMember and not from DisplayMember. The grid works fine and when I select something from this column I see the DisplayMember value, but when the focus gets lost, the grid shows a ValueMember. I have this code combo box column:

statusCBoxColumn.DataSource = dt 'datatable with two fields StatusId and StatusText
statusCBoxColumn.DisplayMember = "StatusText" 'is type NVarchar
statusCBoxColumn.ValueMember = "StatusId" 'is type Int

Can anybody help me?


Edit: I solved this in the following way:

Private Sub dgv_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgv.CellFormatting
  If (dgv.Columns(e.ColumnIndex).Name = "statusCBoxColumn") Then
    If e.Value & "" > "" Then
      Dim s1 As String = e.Value
      e.Value = GetData("Select StatusText from Status where ID = " & e.Value).ToString()
    End If
  End If
End Sub

But I don't think that it's the best solution...


Solution

  • Have you set datapropertyName of the comboboxcolumn and added the column to datagridview? You've to write

        statusCBoxColumn.DataPropertyName = "StatusId";
    

    and add the column like

        datagridview1.Columns.Add(statusCBoxColumn);