Displaying Database information in ListView

I created an address like database with first and last names and include a photo location in the database. Now I want to use ListView to display the photo and name of each row(person) in my database, but I don't know how to retrieve a string from a single cell in my database. can anyone help


Answer this question

Displaying Database information in ListView

  • Nico Vuyge

    Try this (pardon me if the DataRow syntax isn't quite right, although I think I got it right ... it's a little different than the syntax for C#):

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'TODO: This line of code loads data into the 'FirstDatabaseDataSet.Addresses' table. You can move, or remove it, as needed.

    Me.AddressesTableAdapter.Fill(Me.FirstDatabaseDataSet.Addresses)

    For Each currentRow As DataRow In Me.FirstDatabaseDataSet.Addresses

    Me.ListView1.Items.Add(currentRow.Items("MyColumnOne")

    ' and if you have more than one column in your ListView, add this way:

    Me.ListView1.SubItems.Add(currentRow.Items("MyColumnTwo")

    Next

    End Sub



  • Brian2

    Okay, after entering the code that you provided, this is what I got. What am I doing wrong

    Me.ListView1.DataBindings.Add(ListView1, Firstdatabasedataset.Tables[0], FirstName)



  • Thap

    You can not use a datagridview cell in a listview that is why you are getting the error. There is a sample on the vb-tips website on how to load a listview with data from a database.






  • OnCallBI-DBA

    That looks like it might work, but it gives me a large message dialog and says that the logon failed for Northwind. Where do I get the database

  • danial1007

    That's what I'm trying to do, how do I do that

  • Tom Sapp

    hang on - I'm confused now with the requirements.

    the code I had demonstrated was to go through each row of the selected column and add the current row column value into the listview as a normal item - is this not what you wanted

    The reason for this approach is because you cannot really databind the ListView control with the dataset



  • Muzzzy

    try this, untested:



    dim theColumnCollection as DataGridViewColumn
    theColumnCollection = Me.theDataGridView.Columns(columnName)
    for each currentRow as DataGridViewRow in Me.theDataGridView.Rows
    Me.theListView.Items.Add(currentRow.Cells(theColumnCollection.Name))
    next

    does this help



  • shahram11

    Unless this functionality has been added to VS2005 (and I'm not sure that it has), ListView can't be databound ... you have to populate it manually.

  • Palmi

    Okay here's what I have so far....

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'TODO: This line of code loads data into the 'FirstDatabaseDataSet.Addresses' table. You can move, or remove it, as needed.

    Me.AddressesTableAdapter.Fill(Me.FirstDatabaseDataSet.Addresses)

    Dim theColumnCollection As DataGridViewColumn

    theColumnCollection = Me.DataGridViewTextBoxColumn1

    For Each currentRow As DataGridViewRow In Me.DataGridViewTextBoxColumn1.DataGridView.Rows

    Me.ListView1.Items.Add(currentRow.Cells(theColumnCollection.Name))

    Next

    End Sub

    The line in blue is what shows a blue zigzag line underneath it. inside of the small tool tip window, it states that a DataGridViewCell cannot be converted to ListView. Whats going wrong Is there any help documentation to add on to Visual Basic 2005 that tells me where to find this or how to use the datagrid view



  • adorer

    try this:

    theListView.DataBindings.Add(propertyNameOfControl, theDataSet.Tables[0], ColumnName)

    Alternatively I believe you would have to add the items manually to the listview, going through each row of data and picking out the column you wish to add to the listview



  • goldmine

    This article gives you a link to download the northwind database and shows how to install it.


  • John Dunn

    I'm using an SQL database.

    However I have not seen an option to bind data to the ListView component, only an ImageList.

    My plans are to take the first and last name from each row along with the image location, then place the first and last name as the item text in ListView, and the image location to the ImageList. I would then be able to display the image of the person with their name underneath. But I cannot seem to find a way to retrieve the information from a database and place the information progammatically.



  • BilalShouman

    what you may be after is databinding, which is easily done in this case. Simply create a SqlDataAdapter, a dataset and your SqlCommand then execute a query/fill the dataset using the dataAdapter with the record, then bind the UI controls to the dataset by adding a binding to the control.

    you could also use a SqlDataReader to get the specific row of data you like, would be faster and effecient but of course if you are going to modify the details, whilst this is easily achievable, you may be better using the DataAdapter approach

    what database are you using MS Access SQL Server



  • cbueno

    ok you are using VB.NET so....

    Me.ListView1.DataBinding.Add(ListView1, Firstdatabasedataset.Tables(0), "FirstName")

    I still think this will not work, due to the first parameter - it expect a string property name, not an object. However at this stage I am unsure what the parameter should be. The other alternative is the way I had suggested earlier - go through each row and get the current row columns' cell value



  • Displaying Database information in ListView