how to bind two values to combobox

  • 12 years ago
    Hi All I'm binding data to a combobox from database,For Ex: I'm Getting data from CategoryTable,There we have Cat_ID,Cat_Name as fields.I want bind the data to combobox so that i can get two values in the runtime,Cat_Name for displaying and Cat_ID in selected index chaged, How's This possible
  • 12 years ago

    Set ValueMember and DisplayMember respectively. You cannot display both in the ComboBox without concatenation, and this would be pointless if you needed to do anything with that ComboBox data. You can access the CatID by calling SelectedValue and the CatName through SelectedText. SelectedIndex returns the iterative value.

  • 12 years ago
    can you please send me the detailed code if possible
  • 12 years ago

    ?!?

    ComboBox1.ValueMember = MyTable.Columns(0)

    ComboBox1.DisplayMember = MyTable.Columns(1)

  • 12 years ago
    this is my code Public Sub fillComboBox() cn = New SqlConnection(ConStr) ds = New DataSet dv = New DataView Dim ad As New SqlDataAdapter("SELECT * FROM Category", cn) ad.Fill(ds, "Category") dv.Table = ds.Tables("Category") For Each dataRow As DataRow In ds.Tables(0).Rows cmbCategories.DataSource = dv cmbCategories.DisplayMember = "CategoryName" cmbCategories.ValueMember = "CategoryID" cmbCategories.DataBindings.Add("DisplayMember", dv, "CategoryName") cmbCategories.DataBindings.Add(New Binding("SelectedValue", dv, "CategoryID")) Next End Sub still I'm Getting This Error"This causes two bindings in the collection to bind to the same property. Parameter name: binding"
  • 12 years ago

    Your making things far more complicated than needs to be. Scrap the bindings, and just attach the datasource to the combo box DataSource property and set the aforementioned properties.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If Java had true garbage collection, most programs would delete themselves upon execution.” - Robert Sewell