listbox databinding techniques

listbox databinding techniques

Post by Chuck » Sun, 21 Jul 2002 06:54:43

I have a windows form that represents master records in a
database.  One of the fields has lookup values displayed
in a listbox.

What is the best way to fill the listbox from the lookup
  1. Use the listbox's add method in the middle of a
datareader  (while listbox.add )

  2.  bind it to a lookup dataset like this:
       lstBox.DataSource = ds.Tables(0)
       lstBox.DisplayMember = "LicsCode"
  3.  something else

Now I need to set the listbox's initial value and save any
changes after editing to the recordset for the master
table.  What's the best way to do that?  Is there someway
to bind the listbox to the recordset that represents the
row being edited in the master table so that the inital
value and any changes are reflected in the master table

thanks, chuck

p.s. no wizard solutions please, they don't do error
trapping well.
p.s. Is there a book or documentation or references that
describe techniques like this.


listbox databinding techniques

Post by leo lin[M » Thu, 25 Jul 2002 11:47:39

I think the second way is better.

The ComboBox, CheckedListBox, and ListBox controls can be bound to data.
The most common uses of these controls are to browse data in a database, to
enter new data, or to edit existing data.

To bind a ComboBox, CheckedListBox, or ListBox control

Set the DataSource property to a data source object. Possible data sources
include a data table, a data view, a dataset, a data view manager, an
array, or any class that implements the Ilist interface. For more
information, see Providers of Data to Windows Forms.
Set the DisplayMember property to the name of a column in the data source
' Visual Basic
Private Sub BindComboBox()
  ComboBox1.DataSource = DataSet1.Tables("Suppliers")
  ComboBox1.DisplayMember = "ProductName"
End Sub

// C#
private void BindComboBox()
  comboBox1.DataSource = dataSet1.Tables["Suppliers"];
  comboBox1.DisplayMember = "ProductName";


There is KB as follows:;EN-US;Q313482&

There many articles in this KB and you can read it carefully for different

Best Regards


This answer is provided "as is", without any guarantee made as to its
suitability or fitness for any particular use.


1. Databinding of listbox with different tables

I have a requirement where i have to bind listbox
valuemember to child table and displaymember to
parenttable. I have no errors at all but when i add a row
inseated of displaying the actual value corresponding to
the valuemember it shows DataRowView for each entry i add.

Any word on this.....



2. Clear Screen

3. DataBindings ComboBox and ListBox

4. FINALLY....ProE is ported to Linux!!!!

5. Databinding Multiselect Listbox

6. Need help puting Nitros9 kernel in ROM

7. Databound multi-select listbox behavior

8. ANNOUNCEMENT: New OS/2 programming book

9. ATTN: Microsoft - Listbox Databinding Bug?

10. Curious problem when using a databound ListBox with a sorted custom collection class

11. remove last item from databound listbox

12. Navigating a dataset with a databound listbox

13. Databinding listbox in windows forms