Load Values into Listview

Hey there,

I have added a listview on my forms with a maximum of five columns. The next thing I am trying to do is load to values from a table in the database (SQL 7.0) into the correct columns in the listview.
Any help is kindly appreciated.
You can e-mail me directly at david_esof@hotmail.com

Later David.


Comments

  • Hi David,
    A lot depends on just how you are handling the data you are pulling from the database, eg. which data objects you are using. But here's the raw code used to add items and sub-items to a listview. In principle, you can replace the manually created data in the example with data read in from your DB, reading row by (data)row via your data object.
    [code]
    Dim LVItem As ListViewItem
    Dim i As Integer
    With ListView1
    ' Create some dummy data for demo purposes
    ' If
    For i = 0 To 9
    ' Create a new list item object
    LVItem = New ListViewItem
    ' Create some text (= read in from data source)
    LVItem.Text = "Demo Item " & i.ToString

    ' Add it to the ListView
    .Items.Add(LVItem)

    ' Add the sub items (= read in from data source)
    LVItem.SubItems.Add(i.ToString)
    LVItem.SubItems.Add("Third column")
    LVItem.SubItems.Add("Fourth column")
    LVItem.SubItems.Add("Fifth column")
    Next
    End With
    [/code]
    As I say, this is very basic and there are many alternative options depending on how you've constructed your code, but at least it shows the basics of adding sub-items to a listview, (which is usually what catches most people out at first).
    Oh! Don't forget to set the listview View property to Details.
    ================================================================




    : Hey there,
    :
    : I have added a listview on my forms with a maximum of five columns. The next thing I am trying to do is load to values from a table in the database (SQL 7.0) into the correct columns in the listview.
    : Any help is kindly appreciated.
    : You can e-mail me directly at david_esof@hotmail.com
    :
    : Later David.
    :
    :
    :

  • Hi kainsworth,

    On my form I have a button, textboxes and a listview. On form load the list view will contain all the data in the table. When the user inserts data into the textboxes and clicks the button. The data is then saved to a table in the database. When this happens I would like the recent inserted data to be shown in the listview to be updated.

    I am having a problem with extracting values from the database and matching them up with the columns in the listview. I am using the SQL Adapter, and datasets.

    Kainsworth the program that you sent me was good. There were a few properties that I overlooked.

    Later David....
  • Hi David
    One question which immediately occurred to me when you made your original post was this: Does it absolutely have to be a listview? Or did you just want the data displayed in a neat, tabular way? If so, then the DataGrid in place of ListView would make life much easier, as it's specifically designed for the job.
    If you let me know which way you want to go with this, I'll see if I can give you code with a little more detail to get you back on track. I can definitely help with Datagrid (as with most things to do with data handling, the books make it sound easy, but there are still one or two gotchas out there waiting to getcha) :-)
    If it's the listview, it may take me a little longer to find the time to look at it for you, but it is certainly doable with a bit of fiddling around.
    Regards
    Ged
    ============================================================


    : Hi kainsworth,
    :
    : On my form I have a button, textboxes and a listview. On form load the list view will contain all the data in the table. When the user inserts data into the textboxes and clicks the button. The data is then saved to a table in the database. When this happens I would like the recent inserted data to be shown in the listview to be updated.
    :
    : I am having a problem with extracting values from the database and matching them up with the columns in the listview. I am using the SQL Adapter, and datasets.
    :
    : Kainsworth the program that you sent me was good. There were a few properties that I overlooked.
    :
    : Later David....
    :

  • Thanks for replying so fast. I never thought about using a datagrid, because I dred using that control. I struggled with it in VB 6.0 but I will attempt it and get back to you.

    Later David.
  • Hi Kainsworth,

    I have just finished use a datagrid. I still prefer to use the List View. Below is an idea of how I think I can solve my problem. I have implemented the code that you sent me.

    1. Declare String Varibles One variable for every field in the table
    A maximum of 15 variables will be declared.

    2. Using the SQL adapters & SQL Commands, I will then extract each field and assign it to a variable.

    3. Then using the code that you sent me I will inserted the data into the list view.


    Therefore I will extract one row of data from the table and load it into the listview. A loop will have to be used to increment the rows.

    The Problem:
    Using the SQL Commands (SELECT) I cannot extract the values from the database and store them in the variables.
    If I use ADODB then it works excellent, but I want to use SQL Adapter and its SQL Commands.
    Below is some of the code that I have attempted and got no results:

    Public Sub Extract()
    Dim Nam As String
    With Me.datAgeRestriction.SelectCommand
    .CommandText = "SELECT AGR_CODE FROM AGE_RESTRICTION"
    .CommandType = CommandType.Text
    AgeRest = .Parameters("@AGR_CODE").Value
    End With
    Me.connVideo.Open()
    Try
    Me.datAgeRestriction.SelectCommand.ExecuteNonQuery()
    Catch ex As Exception
    MessageBox.Show(ex.Message)
    MessageBox.Show("Keep On Trying David!!!!!!!")
    End Try
    connVideo.Close()
    MsgBox(AgeRest)
    End Sub

    In the database I have a table called AGE_RESTRICTION. I am trying the retrieve that code and store it in AgeRest.

    Later David...

    P.S Kainsworth the datagrid works, but I think that the List view will look me professional.

  • For demo purposes I've pulled the data directly from the database to populate the listview. In your project, you could use the same logic to update the listview from a dataset - refreshing the listview whenever the dataset is amended by the user making changes to the (databound?) textboxes you mentioned earlier.
    There are many approaches (some may be better than the one below) , but this one does demonstrate how you can use a SELECT command to pull your data into a DataSet and use (all or parts of) that data to populate your listview.
    [code]
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.SqlConnection1.Open() ' A Conn object created via the ToolBox

    Dim DSAuthors As DataSet = New DataSet
    ' (Data Adapter created from the toolbox)
    ' (Data Adapter's select command simply selects all data from
    ' Authors table of the Microsoft Pubs database
    SqlDataAdapter1.Fill(DSAuthors)

    SqlConnection1.Close()

    ' here's the bit that populates the ListView:
    Dim authorRow As DataRow
    Dim authorColumn As DataColumn
    For Each authorRow In DSAuthors.Tables(0).Rows
    For Each authorColumn In DSAuthors.Tables(0).Columns
    With ListView1
    Dim LVItem As ListViewItem = New ListViewItem
    If authorColumn.Ordinal = 0 Then ' First column item
    ' Create a new listviewitem object for first col
    LVItem.Text = authorRow.Item(0)
    ' Add it to the ListView
    .Items.Add(LVItem)
    End If
    ' it's not a column 0 item, therefore it's a subitem
    LVItem.SubItems.Add(authorRow.Item(2).ToString)
    LVItem.SubItems.Add(authorRow.Item(1).ToString)
    LVItem.SubItems.Add(authorRow.Item(5).ToString)
    ' I've switched order of DataSet columns above just to show that
    ' you do have flexibility of what you read in and where you
    ' display it. (you could of course do the filtering and column
    ' juggling in your Select command.
    End With
    Next
    Next
    End Sub
    [/code]
    Hope it helps
    Ged

  • Hey Ged,

    Thank you for the code that you sent me. I have implemented it and at the rate that I am going I am not jumping over hurdles but leaping over them.

    Anyway I have a question for you. On one of my forms I have a combo box that retrieves data from the database. After inserting data into the rest on the controls on the form, I then save.
    When I save I get an error message saying: Object must be IConvertible
    I soon discovered that the error was in the the that saves to the new table. The statement that gave the error is:

    .Parameters("@MOC_DESC").Value=cboMocDesc.SelectedItem

    now when I change the above line of code to:

    .Parameters("@MOC_DESC").Value=cboMocDesc.Text.ToString

    Everything runs fine. What is the difference between the cboMocDesc.SelectedItem and the cboMocDesc.Text.ToString?

    Later David...

    P.S. Thank you again for that code.
  • Hi David
    I think it will be because the Parameter requires a String Type (which it gets when you explicitly use the ToString method in your second example); in the first example, I believe it deals with it as a generic object. Even though we humans read the SelectedItems' value as string/text, the compiler doesn't necessarily do that. As far as I can remember, comboboxes and listboxes deal with all items which are added to them as generic objects. This enables you to create your own types and display them in listboxes/combos.
    If you haven't already tried this experiment, you could try
    [code]
    cboMocDesc.SelectedItem.ToString
    [/code]
    which I would expect to have the same effect. If you have a go at this, you'll discover another of the VS gotchas - Intellisense doesn't offer you the ToString method as a choice in its dropdown list, only GetType. There are many cases where this happens, and it's worth experimenting to see what additional methods are available to you in situations like this one.
    Cheers
    Ged

    =============================================================

    : Hey Ged,
    :
    : Thank you for the code that you sent me. I have implemented it and at the rate that I am going I am not jumping over hurdles but leaping over them.
    :
    : Anyway I have a question for you. On one of my forms I have a combo box that retrieves data from the database. After inserting data into the rest on the controls on the form, I then save.
    : When I save I get an error message saying: Object must be IConvertible
    : I soon discovered that the error was in the the that saves to the new table. The statement that gave the error is:
    :
    : .Parameters("@MOC_DESC").Value=cboMocDesc.SelectedItem
    :
    : now when I change the above line of code to:
    :
    : .Parameters("@MOC_DESC").Value=cboMocDesc.Text.ToString
    :
    : Everything runs fine. What is the difference between the cboMocDesc.SelectedItem and the cboMocDesc.Text.ToString?
    :
    : Later David...
    :
    : P.S. Thank you again for that code.
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories