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
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.
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.
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
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.
Re: create multicolumns ListBox
Thank you for your reply, jmcilhinney.
It works now :D. 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:
mycombobox.DataSource = objDataSet
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
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.
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:
...
objDataAdapter.SelectCommand.CommandType = CommandType.Text
Dim objDataTable As DataTable = objDataSet.Tables("authors")
' 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"
mycombobox.DataSource = objDataTable
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
Re: create multicolumns ListBox
When you execute this line:
VB Code:
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:
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.