swdev
swdev

Reputation: 5147

Can't programmatically select an item in DataGridViewComboBoxColumn?

I had a DataGridView, and already able to add DataGridViewComboBoxColumn to it. It read data from database StoredProcedure using custom ValueDescriptor class, this is the code for it:

Shared Sub fillComboBoxCellUsingSP(ByVal comboBox As DataGridViewComboBoxColumn, ByVal proc_name As String, ByVal param As Object(), ByVal firstitem As String)
  Dim dt As New DataTable
  Utils.executeSP(proc_name, param, dt)
  If comboBox.DataSource IsNot Nothing Then
    comboBox.DataSource = Nothing
  End If
  comboBox.Items.Clear()

  Dim VDP_Array As New ArrayList
  VDP_Array.Add(New ValueDescriptionPair(Nothing, firstitem))
  For Each row As DataRow In dt.Rows
    VDP_Array.Add(New ValueDescriptionPair(row(0), row(1)))
  Next
  With comboBox
    .DisplayMember = "Description"
    .ValueMember = "Value"
    .DataSource = VDP_Array
  End With
  dt.Dispose()
End Sub

It can display the data alright, but I can't select it programmatically, using this method:

Shared Sub selectInComboDataGrid(ByVal comboBox As DataGridViewComboBoxCell, ByVal value As String)
  For Each o As ValueDescriptionPair In comboBox.Items
    If o.Value IsNot Nothing AndAlso o.Value.ToString.Equals(value) Then
      comboBox.Value = o
      Exit For
    End If
  Next
End Sub

Actually, the line that read comboBox.Value = o is okay. But still, the combo box cell didn't display the value. Just empty. And sometimes, DataError event raised.

Is there any clue for this? Thank you so much in advance.

Has been try to solve this for almost two hours... :)

Upvotes: 0

Views: 2024

Answers (1)

Flaick
Flaick

Reputation: 232

Try to set the value to the DataGridView and not to ComboBox. Here an example:

comboBox.DataGridView(comboBox.ColumnIndex, comboBox.RowIndex).Value = o

Upvotes: 1

Related Questions