[RESOLVED] combo box binding problem
hi all,
i'm having a problem binding my combo box to my data table
i have these codes that binds my combo box.
Code:
combo.DataBindings.Add("Text", DataTable, "status")
this code works well only when reading data... it automatically selects the correct item in my combo box to whatever was saved to that column in my table... however, when editing or updating the combo box text, it doesn't update.
so then i tried these codes
Code:
combo.DataSource = Datatable
combo.DisplayMember = "status"
combo.Items.Add("MARRIED")
combo.Items.Add("SINGLE")
combo.Items.Add("WIDOWED")
unfortunately this won't work with the reading and updating.
considering that i have all my adapter insert, update, delete command set already and are actually working with databinds in a textbox control.
any thoughts? thanks...
Re: combo box binding problem
The ComboBox supports both types of binding: complex and simple. It supports complex binding via its DataSource property, and it supports simple binding like any other control via its DataBindings collection. You first have to decide what type or types of binding you need to implement. In your case it should be simple binding. If your list of marital statuses is drawn from a database table then you'd use complex binding too, but I don't think that's the case here.
The first step is to add the list of items to the ComboBox. If you're using a predefined list of strings then you should be adding them in designer. If it's an unknown list of strings or a list of some other type then you should add them in code, e.g.
vb.net Code:
combo.Items.Add("MARRIED")
combo.Items.Add("SINGLE")
combo.Items.Add("WIDOWED")
You should also be setting the DropDownStyle property to DropDownList in the designer.
Now you need to apply your simple binding. The idea here is that the item selected in the ComboBox corresponds to 'status' field in the DataRow:
vb Code:
combo.DataBindings.Add("SelectedItem", DataTable, "status")
Re: combo box binding problem