Results 1 to 12 of 12

Thread: Populating listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    2

    Populating listbox

    Hello!

    I am fairly new to VB, but I am picking things up quickly and very much enjoying coding with VB 2005.

    I have a problem populating a Listbox... I wish to populate this listbox using a sql command. I have a feeling it maybe the event which is wrong but being a newb I'm sure someone will correct me.

    Code:
        Private Sub lstEngineer_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstEngineer.SelectedIndexChanged
    
            sql = "select * from Engineers where Engineers.Busy = '0'"
    
            Dim commandUpdate As New OdbcCommand(sql)
            Using connection As New OdbcConnection(constring)
    
                commandUpdate.Connection = connection
                connection.Open()
                commandUpdate.ExecuteNonQuery()
    
            End Using
    Thanks very much!

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Populating listbox

    Try it from the click event instead.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    2

    Re: Populating listbox

    Hmmm, I tried it as a click although I wish this to be populated when the form is loaded so will I need add something in the loading of the form?

    I'd ideally like to have this populated once the form loads as it will be showing the status of 'engineers' and their availability.

    Thanks in advance...

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Populating listbox

    I don't see any mention of a ListBox there at all, so there's no wonder the ListBox doesn't get populated. Follow the Data Access Examples link in my signature and take a look at the example for displaying data without updating. Once you can populate a DataTable with your data then you can simply bind it to your ListBox, e.g.
    vb.net Code:
    1. myListBox.DisplayMember = "name of column to display here"
    2. myListBox.ValueMember = "name of ID column here"
    3. myListBox.DataSource = myDataTable
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: Populating listbox

    I wrote the following procedure which sorts a data table, filters the data table according to specific criteria and populates a list box control with the resulting view as its data source.
    Code:
     
    Public Sub FillListBox(ByVal myListBox As DevExpress.XtraEditors.ListBoxControl, _
                        ByVal myDataTable As DataTable, ByVal sDisplayField As String, _
                        ByVal sItemDataField As String, ByVal sDataViewSortString As String, _
                        ByVal sDataViewRowFilter As String)
    
            'sorts the data table according to sDataViewSortString
            'filters the data table according to sDataViewRowFilter
            With myDataTable.DefaultView
                .Sort = sDataViewSortString
                .RowFilter = sDataViewRowFilter
            End With
    
            With myListBox
                .Items.Clear()
                .DisplayMember = sDisplayField
                .ValueMember = sItemDataField
                .DataSource = myDataTable.DefaultView
            End With
    
        End Sub
    It works OK when there is only one list box control on the form but I start getting strange results when I have two list boxes on the same form and I try to populate them in succession with different row filters applied to the same data table. Both list boxes are populated with the same data even though the passed row filter is different and the ListBoxControl1.Items.Count property returns zero even though the list box is populated with data.

    Note that my list box controls are of type DevExpress.XtraEditors.ListBoxControl which probably makes no difference but JM I notice that you are now promoting these products.

  6. #6
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: Populating listbox

    There is something aspect of the Data View object that I am missing as this amended version gives me the correct output.

    Using the list box control's ItemCount property rather than its Items.Count property gives me the correct number of items in the list box.

    Code:
    Public Sub FillListBox(ByVal myListBox As DevExpress.XtraEditors.ListBoxControl, _
                        ByVal myDataTable As DataTable, ByVal sDisplayField As String, _
                        ByVal sItemDataField As String, ByVal sDataViewSortString As String, _
                        ByVal sDataViewRowFilter As String)
    
            'sorts the data table according to sDataViewSortString
            'filters the data table according to sDataViewRowFilter
            Dim myDataView As DataView = myDataTable.DefaultView
    
            With myDataView
                .Sort = sDataViewSortString
                .RowFilter = sDataViewRowFilter
            End With
    
            With myListBox
                .Items.Clear()
                .DisplayMember = sDisplayField
                .ValueMember = sItemDataField
                .DataSource = myDataView.ToTable
            End With
    
        End Sub

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Populating listbox

    The aspect you're missing is the fact that if you bind the same DataView to two controls then both controls will show the same data. A DataTable's DefaultView is just one DataView object. If you filter the DefaultView and bind it, then filter it again and bind it again, the control you bound to first will update its display to reflect the new filter. One DataView can only show one set of data at a time.

    That new code is creating a whole new DataTable from that DataView. Two DataTables means two sets of data.

    What you should be doing is not mucking about with DataViews at all. That's for .NET 1.x. In .NET 2.0 you should be binding a different BindingSource to each set of controls, then binding the one DataTable to each BindingSource. You then set the Filter property of each BindingSource to a different value and each set of controls will reflect it. Voila.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: Populating listbox

    OK. I rewrote the procedure using a locally declared BindingSource object but now I am back to where I started. It works with one List Box but not two. Does the BindingSource object have to persist beyond the procedure?

    Code:
    Public Sub FillListBox(ByVal myListBox As DevExpress.XtraEditors.ListBoxControl, _
                        ByVal myDataTable As DataTable, ByVal sDisplayField As String, _
                        ByVal sItemDataField As String, ByVal sDataViewSortString As String, _
                        ByVal sDataViewRowFilter As String)
    
            'sorts the data table according to sDataViewSortString
            'filters the data table according to sDataViewRowFilter
            Dim myBindingSource As New BindingSource
    
            With myBindingSource
                .DataSource = myDataTable
                If sDataViewSortString.Length > 0 Then .Sort = sDataViewSortString
                If sDataViewRowFilter.Length > 0 Then .Filter = sDataViewRowFilter
            End With
    
            With myListBox
                .Items.Clear()
                .DisplayMember = sDisplayField
                .ValueMember = sItemDataField
                .DataSource = myBindingSource
            End With
    
            myBindingSource = Nothing
    
        End Sub

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Populating listbox

    You have two ListBoxes added to your form in the designer no doubt. Why would you not add your BindingSources in the designer too?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: Populating listbox

    I added two BindingSource controls to the designer and passed each to a FillListBox procedure (one for each list box) but I am still getting the same result.
    Code:
    Public Sub FillListBox(ByVal myListBox As DevExpress.XtraEditors.ListBoxControl, _
                        ByVal myBindingSource As BindingSource, _
                        ByVal myDataTable As DataTable, ByVal sDisplayField As String, _
                        ByVal sItemDataField As String, ByVal sDataViewSortString As String, _
                        ByVal sDataViewRowFilter As String)
    
            'sorts the data table according to sDataViewSortString
            'filters the data table according to sDataViewRowFilter
            With myBindingSource
                .DataSource = myDataTable
                If sDataViewSortString.Length > 0 Then .Sort = sDataViewSortString
                If sDataViewRowFilter.Length > 0 Then .Filter = sDataViewRowFilter
            End With
    
            With myListBox
                .Items.Clear()
                .DisplayMember = sDisplayField
                .ValueMember = sItemDataField
                .DataSource = myBindingSource
            End With
    
        End Sub

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Populating listbox

    OK, my humblest apologies for feeding you incorrect information. I was thinking I had done it this way before but I mustn't have because, as you're seeing, it just doesn't work like that. I've been using business objects myself lately so I haven't bound any DataTables directly for a while. Again, sorry for wasting your time.

    Even with BindingSources, if you want more than one view of a DataTable then you need to create at least one DataView explicitly. I'd suggest using the DefaultView if you only want one view but creating all views explicitly if you want more than one. You should keep the BindingSources though. Just change this:
    vb.net Code:
    1. .DataSource = myDataTable
    to this:
    vb.net Code:
    1. .DataSource = New DataView(myDataTable)
    and it will work properly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: Populating listbox

    The following works. I don't need to add a BindingSource control to the form and pass it to my procedure - I can create it on the fly.

    Code:
    Public Sub FillListBox(ByVal myListBox As DevExpress.XtraEditors.ListBoxControl, _
                        ByVal myDataTable As DataTable, ByVal sDisplayField As String, _
                        ByVal sItemDataField As String, ByVal sSortString As String, _
                        ByVal sRowFilter As String)
    
            'sorts the data table according to sSortString
            'filters the data table according to sRowFilter
            Dim myBindingSource As New BindingSource
    
            With myBindingSource
                .DataSource = New DataView(myDataTable)
                If sSortString.Length > 0 Then .Sort = sSortString
                If sRowFilter.Length > 0 Then .Filter = sRowFilter
            End With
    
            With myListBox
                .Items.Clear()
                .DisplayMember = sDisplayField
                .ValueMember = sItemDataField
                .DataSource = myBindingSource
            End With
    
            myBindingSource = Nothing
    
        End Sub
    I can then retrieve the item data for the selected item with:

    Code:
    Dim sItemData As String = ListBoxControl1.SelectedValue
    This is a lot tidier compared to what I have used in the past to achieve the same thing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width