|
-
Nov 16th, 2009, 12:57 PM
#1
Thread Starter
Junior Member
ListView control - adding items from a SQL database.
Hey I was wondering whether anyone could help me with this problem. I am very new to Visual Basic, and I need to add items from my database to the ListView control.
I have a table in the database called tblChannels that I would like to draw the information from and populate the ListView control in VB, in the form of a column.
I currently am unsure about how to go about doing this. Previously I have created combo boxes that draw the information from the database, but trying this method for the ListView does not work how I want it to.
Sorry if I have not included enough information, let me know if you need any additional details.
Thanks.
-
Nov 16th, 2009, 01:42 PM
#2
Addicted Member
Re: ListView control - adding items from a SQL database.
You could use a SqlDataReader to get the information from your database, and with each read you would add the information to your ListView.
If someone has helped you, please make sure to rate them. 
-
Nov 16th, 2009, 01:57 PM
#3
Thread Starter
Junior Member
Re: ListView control - adding items from a SQL database.
I was kind of hoping for a bit more in depth help, if that is possible. As I said I am very new to all this. :/
-
Nov 16th, 2009, 02:45 PM
#4
Addicted Member
Re: ListView control - adding items from a SQL database.
Here is actually 2 simple ways -- not using the sql datareader that I suggested. The function MakeTableMenu returns a DataTable object that I use in the rest of the code. On the form I am using a ListBox and a ListView object.
The sql datareader could be used in place of a DataTable object for the list view (as well as the listbox) control. In that case you would loop and as long as there were rows to read you would add the information as the new listviewitem.
VBNET Code:
Dim dtSource As New DataTable
dtSource = MakeTableMenu("SingleColumnListOfData", 1, 6)
' ### ListBox
ListBox1.DataSource = dtSource
ListBox1.DisplayMember = "Col0"
ListBox1.ValueMember = "Col0"
' ### ListBox
' ### ListView
' column
ListView1.Columns.Add("My Items")
'first column items to add to listview
Dim lviItem As ListViewItem
For Each dr As DataRow In dtSource.Rows
lviItem = New ListViewItem("Item:" & dr(0).ToString())
ListView1.Items.Add(lviItem)
Next
'### ListView
ListView
ListBox
If someone has helped you, please make sure to rate them. 
-
Nov 16th, 2009, 02:52 PM
#5
Thread Starter
Junior Member
Re: ListView control - adding items from a SQL database.
I'm really sorry but I don't understand. :/
-
Nov 16th, 2009, 04:02 PM
#6
Re: ListView control - adding items from a SQL database.
What part are you having trouble with:
- Retrieving data from a database without using databinding, or
- Displaying data in a ListView.
If it's the first, there's an excellent codebank submission (and probably more than one) by jmcilhinney that shows you exactly how to do that. You would need a bit of SQL knowledge and use it to get a DataReader.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|