Data Binding to a listbox and label

JohnV

Active member
Joined
Feb 20, 2015
Messages
27
Programming Experience
3-5
Hi, I have a requirements to bind MS Access records and binding to a listbox and Label. I have two column a RateCode and RateDescription. Once i choose a ratecode the description should be displayed. For ex. I choose number 3 from the list the equivalent description "Exceed" will be show in lable or textbox. thank you very much.

Sample:

RateCode---RateDescription
-------------------------------
1----------Normal
2----------Meet
3----------Exceed
4----------Strong
5----------Excel
 
Populate a DataTable with your data and then bind it like this:
myListBox.DisplayMember = "RateCode"
myListBox.DataSource = myDataTable
myLabel.DataBindings.Add("Text", myDataTable, "RateDescription")
 
Thank you very much Jim.
Here is the working modified code.
      Dim str As String = KPI.accdb")
        Dim con As New OleDb.OleDbConnection(str)
        Dim cmd As String = "SELECT RateCode, RateDescription FROM tblRating"
        Dim adpt As New OleDbDataAdapter(cmd, con)
        Dim myDataset As New DataSet

        adpt.Fill(myDataset, "tblRating")
        Dim myDatatable As DataTable = myDataset.Tables(0)
        Dim tempRow As DataRow

        For Each tempRow In myDatatable.Rows
            ListBox1.Items.Add(tempRow("RateCode"))
        Next

        'Dim dt As DataTable = SimilateLoadingData()
        ListBox2.DisplayMember = "RateCode"
        ListBox2.DataSource = myDatatable

        'Bind to RateCode column of Rating table
        Label1.DataBindings.Add("text", myDatatable, "RateDescription")
 
May I recommend not using a DataSet when all you need is a DataTable, i.e. this:
  Dim myDataset As New DataSet
 
  adpt.Fill(myDataset, "tblRating")
  Dim myDatatable As DataTable = myDataset.Tables(0)
would become this:
  Dim myDatatable As DataTable
 
  adpt.Fill(myDatatable)
Also, why are you this at all:
  Dim tempRow As DataRow
 
  For Each tempRow In myDatatable.Rows
      ListBox1.Items.Add(tempRow("RateCode"))
  Next
 
When I try to use this code. The adpt.fiil(myDatatable) was underline by color green and it said variable "myDatatable" is used before it has been assigned a value and no records displayed in my listbox.


Dim myDatatable As DataTable
 
adpt.Fill(myDatatable)


for this portion, I use this code to add the records from ms access table or populate the ms access records to listbox. If this portion is wrong waht should be the correct coding. thanks.
Dim tempRow As DataRow
        For Each tempRow In myDatatable.Rows
            ListBox1.Items.Add(tempRow("RateCode"))
        Next
 
Last edited:
Sorry, that should have been:
Dim myDatatable As New DataTable
 
I got this error after the data entry and i close the form. When trying to open again the form this error appear.
Item collection cannot be modifed when the datasource is set.
 
for this portion, I use this code to add the records from ms access table or populate the ms access records to listbox. If this portion is wrong waht should be the correct coding. thanks.
Dim tempRow As DataRow
        For Each tempRow In myDatatable.Rows
            ListBox1.Items.Add(tempRow("RateCode"))
        Next

That's what this does:
  ListBox2.DisplayMember = "RateCode"
  ListBox2.DataSource = myDatatable
 
I got this error after the data entry and i close the form. When trying to open again the form this error appear.
Item collection cannot be modifed when the datasource is set.

Exactly, so don't do it. You can't set the DataSource property and Add to or Remove from the Items collection directly. If you want to add and remove items then use the data source.
 
actually i'm using this to displayed the ratings and tick or select a desired ratecode with corresponding Ratedescription. what should i do to fixed this error. thank you.

Untitled.jpg
 
Why does your code include two ListBoxes yet your screenshot only shows one? If you can't be clear about the issue then you're unlikely to get the help you need because it's a waste of our time to try to sort out what's relevant and what's not.
 
Sorry Jim, i copied a code from my sample form not from the orignal form. Its only one listbox. below is the modified code and its working now. the error was not displayed anymore but another error appear. it say a "column Name ItemNumber already belong to this datatable". the cursor point to this code

dt.Columns.Add("ItemNumber", GetType(String))




 Dim SQLAdapter As OleDbDataAdapter = Nothing
        Dim str As String = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Desktop\FeedBackSystem\FBSystems\FBSystems\Data\KPI.accdb")
        Dim con As New OleDb.OleDbConnection(str)
        Dim cmd As String = "SELECT RateCode, RateDescription FROM tblRating"
        Dim adpt As New OleDbDataAdapter(cmd, con)
        'Dim myDataset As New DataSet

        Dim myDatatable As New DataTable

        adpt.Fill(myDatatable)

        ListBox1.DataSource = Nothing
        ListBox1.Items.Clear()

        Dim tempRow As DataRow
        For Each tempRow In myDatatable.Rows
            ListBox1.Items.Add(tempRow("RateCode"))
        Next

        'Dim dt As DataTable = SimilateLoadingData()
        ListBox1.DisplayMember = "RateCode"
        ListBox1.DataSource = myDatatable

        Label9.DataBindings.Clear()

        'Bind to RateCode column of Rating table
        Label9.DataBindings.Add("text", myDatatable, "RateDescription

 
Last edited:
hi jim. just fixed the issue by adding this code before the DataGridview setup. Thank you very much.
dt.Columns.Clear()
 
Back
Top