4

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...

Korem
  • 11,383
  • 7
  • 55
  • 72
wertyk
  • 410
  • 10
  • 30

5 Answers5

1

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);
Sukanya
  • 1,041
  • 8
  • 21
  • 40
  • Yes, DataPropertyName is seted and the column is added to datagridview. The datagridview work fine and i can change value of this column but when I change focus from this grid it's show ValueMember not DisplayMember in the ComboBoxColumn. – wertyk May 30 '12 at 09:15
1

I had the same problem. The "statusID" for me is long. I made it by system.type.gettype("System.Int64") That gave the same behaviour. When changing it to Int32, the behaviour was OK.

So it was à mismatch between valuemember type and datagridview type.

Rob
  • 11
  • 1
1

I know this is a 10 years old question, but I was having same problem and I found out that according to MSDN :

When the DataSource property is set to a string array, the ValueMember property does not need to be set because each string in the array will be used as a valid display string and as a valid underlying value.

evry1falls
  • 194
  • 4
  • 15
0

Try this one I've also have a problem on displaying value member,hope it will work.

    For index = 0 To cbo.Items.Count - 1
        cbo.SelectedIndex = index 
        MessageBox.Show(CType(cbo.SelectedValue, String))
    Next

What i had used is:

    Dim jm As Integer
    jm = CType(cmbCategory.SelectedValue, String)
    MsgBox(jm)
NoNaMe
  • 6,020
  • 30
  • 82
  • 110
0

e.Value = GetData("Select to_char(StatusId) , StatusText from Status where ID = " & e.Value).ToString()

  • 1
    Hi @skorec1, and welcome to SO! Can you explain this a little bit more? Does it solve the problem? What is the problem? Just more detail is all. Also, please enclose that in a code block ("{}" button) so it's easier to read. – Dylan Corriveau Oct 21 '14 at 19:17