Results 1 to 9 of 9

Thread: create multicolumns ListBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    14

    create multicolumns ListBox

    Hi,

    I was wondering if i could get some help here. I'm new to VB.NET and am learning my way around it. i was desperate to want the listbox to show multicolumns but couldnot find a way to do it.

    So i was wondering if there is a way of displaying multicolumns listbox and also wonder if there are good VB .NET books around that explains well on the development of VB applications.

    Thank you in advance

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

    Re: create multicolumns ListBox

    You could create your own ListBox that displayed multiple columns but it would take a fair bit of work and there is no need. Use a ListView with its View property set to Details or else use a DataGridView.

    There are plenty of good books around but before you spend money I'd suggest you check out the multitude of tutorials available on the Net. There are links to a few good ones in my signature but there are others 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

  3. #3
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: create multicolumns ListBox

    Look for any VB.NET book from Microsoft Press - they have several levels depending on your starting skill.

    Instead of using a listbox you should try the ListView control. Set the View property to Details and it will show tabular data. You can add columns through the Columns property. Each item in a ListView is not a simple object like the ListBox but a ListViewItem which exposes a SubItems property. SubItems allows access to the rest of the columns as well as the first column (the first column technically isn't a subitem, but that's just the way SubItems works). Index 0 is the first column, index 1 the second, etc. Hope that helps.
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    14

    Re: create multicolumns ListBox

    Thank you to you both, DNA7433 & jmcilhinney for your reply.

    All right, i saw some samples that actually use DataGridView. I'll try to use ListView later on. However, I've got problem getting it to work.

    The error is "an unhandled exception of type 'system.nullreferenceexception' occurred in pubs.exe". Object reference not set to an instance of an object.

    Here is my code

    Dim objConnection As SqlConnection = New SqlConnection("Server=localhost;Database=pubs;Integrated Security=true")

    Dim grdAuthorTitles As DataGrid
    Dim objDataAdapter As New SqlDataAdapter
    Dim objDataSet As DataSet = New DataSet

    ' Set the SelectCommand properties...
    objDataAdapter.SelectCommand = New SqlCommand
    objDataAdapter.SelectCommand.Connection = objConnection
    objDataAdapter.SelectCommand.CommandText = "SELECT au_lname, au_fname, title, price " & _
    "FROM authors " & _
    "JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
    "JOIN titles ON titleauthor.title_id = titles.title_id " & _
    "ORDER BY au_lname, au_fname"

    objDataAdapter.SelectCommand.CommandType = CommandType.Text

    ' Open the database connection
    objConnection.Open()

    ' Fill the DataSet object with data
    objDataAdapter.Fill(objDataSet, "authors")

    ' Close the database connection
    objConnection.Close()

    ' Set the DataGrid properties to bind it to our data
    grdAuthorTitles.DataSource = objDataSet
    grdAuthorTitles.DataMember = "authors"

    i think the errors may be to do with the way i declare the grdAuthorTitles as datagrid or dataset is empty.

    How do i get around this problem?

    Thank you in advance

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

    Re: create multicolumns ListBox

    A DataGrid is NOT a DataGridView. They are both grid controls for displaying data but they are NOT the same thing. You shouldn't be declaring any grids in code. You should have added a DataGridView to your form in the design window and then you just refer to it by the name you gave it there.

    Also, please wrap your code snippets in [Highlight=VB] tags in future. There's a button on the editor that will do it for you.
    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    14

    Re: create multicolumns ListBox

    Thank you for your reply, jmcilhinney.

    It works now . It's my first test and works. thanks for the help.

    I got the datagridview working and like to try out the combobox. so i put the following code after the "grdAuthorTitles" as follows

    VB Code:
    1. mycombobox.DataSource = objDataSet
    2. mycombobox.DisplayMember = objDataSet.ToString()

    When i hit F5, i got system.data.dataview.... in the combobox. Do i miss anything here?

    Thank you in advance

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

    Re: create multicolumns ListBox

    The DisplayMember is the name of the property you want displayed. If you bind a DataSet to a control then the DisplayMember must be of the form "TableName.ColumnName". If you bind a DataTable then you don't need the table name, so it would just be "ColumnName".

    If you're using VB 2005 then may I also suggest using a BindingSource as an intermediate step between the data and the control. You don't have to but it's a good idea becase it does make doing some things easier.
    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

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    14

    Re: create multicolumns ListBox

    Thanks jmcilhinney for your reply.

    No, i'm still not getting the combobox to display the author lastname. I use the datatable as follows

    VB Code:
    1. ...
    2.         objDataAdapter.SelectCommand.CommandType = CommandType.Text
    3.  
    4.         Dim objDataTable As DataTable = objDataSet.Tables("authors")
    5.  
    6.         ' Open the database connection
    7.         objConnection.Open()
    8.  
    9.         ' Fill the DataSet object with data
    10.         objDataAdapter.Fill(objDataSet, "authors")
    11.  
    12.         ' Close the database connection
    13.         objConnection.Close()
    14.  
    15.         ' Set the DataGrid properties to bind it to our data
    16.         grdAuthorTitles.DataSource = objDataSet
    17.         grdAuthorTitles.DataMember = "authors"
    18.  
    19.         mycombobox.DataSource = objDataTable
    20.         mycombobox.DisplayMember = "au_fname"

    No, I'm using VB studio .NET 2003. I could use VB 2005 but because my client uses SQL Server 2000 SP4, i have to use something that works well with that SQL Server.

    By the way, i'm not sure if i could set the combobox to display more than one column like in Access.

    Thank you in advance

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

    Re: create multicolumns ListBox

    When you execute this line:
    VB Code:
    1. Dim objDataTable As DataTable = objDataSet.Tables("authors")
    does the DataSet even contain a table named "authors"? I'd say probably not, and the table isn't being created until you call this line:
    VB Code:
    1. objDataAdapter.Fill(objDataSet, "authors")
    By the way, there's no issue using SQL Server 2000 from a VB 2005 app. Do you really think that they would create .NET 2.0 and VS 2005 so that all the thousands of people running SQL Server 2000 couldn't use it?

    Finally, ComboBoxes display a single column of data in their drop-down list. Search Google and you'll find third-party combo boxes that do support multiple columns, both free and paid for.
    Last edited by jmcilhinney; Sep 11th, 2006 at 02:06 AM.
    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

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