Filling a ComBox manually with DisplayMember and ValueMember

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi!

When I need to have DisplayMember and ValueMember available in a ComboBox I
give a DataView or a DataTable to Combo's DataSource property. But, now, I
need to fill a combo quickly with 2 or 3 static items. How can I fill that
combo manually without giving DataView/DataTable to the combo's DataSource.
Is there any way to do that through Array ou String and have Display/Value
member available?

Thanks!
 
If I understand you correctly you can do a few things. Yes, you can bind to
anything that implements IList - so an arraylist, array etc will work. Or
you can just add those static items to the data structure that you're
binding to - so you can just add a datarow for each of the items that are
static, then bind to it. Either way will work.

HTH,

Bill
 
Thanks for your answer.

Actually I've the following situation:

I've definied a class that holds the two properties

----
Public Class ComboBoxValues
Private strDisplayMember As String
Private strValueMember As String

Public Sub New(ByVal DisplayMember As String, ByVal ValueMember As String)
MyBase.New()
Me.strDisplayMember = DisplayMember
Me.strValueMember = ValueMember
End Sub

Public ReadOnly Property DisplayMember() As String
Get
Return strDisplayMember
End Get
End Property

Public ReadOnly Property ValueMember() As String
Get
Return strValueMember
End Get
End Property

Public Overrides Function ToString() As String
Return Me.DisplayMember & " - " & Me.ValueMember
End Function
End Class
----

Then I added the values and bound to combobox

----
Dim myArrayList As New ArrayList
myArrayList.Add(New ComboBoxValues("Value1", "1"))
myArrayList.Add(New ComboBoxValues("Value2", "2"))
myArrayList.Add(New ComboBoxValues("Value3", "3"))

ComboBox1.DataSource = myArrayList
ComboBox1.DisplayMember = "DisplayMember"
ComboBox1.ValueMember = "ValueMember"
----

After that, tried to bind comboxbox with the DataSet

----
ComboBox1.DataBindings.Clear()
ComboBox1.DataBindings.Add(New Binding("SelectedValue",
DataSet1.Tables("myTableMapping").DefaultView, "Values"))
----

But, I don't know why, it doesn't work. I've the static values in the
combobox but its DataBindings isn't binding the property SelectedValue. It's
always in blank state.

Could you give some tips to solve this issue?

Thanks again
 
Back
Top