Several ways to load a ListView

We will see different ways to load a listview in visual basic. net and in this example we will always do from a DataTable.

A listview in an object, which in turn is a collection of objects ListViewItems, so if you want to fill a ListView, the first thing we must do is define the ListViewItem's that use to be adding to the ListView.

We will not see how it fills the DataTable. Dt is called supenemos that already contains data. His fields are Name, Last Name and Age.

Consider the entire process step by step:

 For i As Integer = 1 To dt.Rows.Count - 1

     "I think the ListViewItem (item)
     Dim item As ListViewItem

     "I think a new instance of item data by passing the name as principal
     item = New ListViewItem (dt.Rows (i). Item ("Name"). ToString)

     'I add the other data to SubItems of item
     item.SubItems.Add (dt.Rows (i). Item ("Name"). ToString)
     item.SubItems.Add (dt.Rows (i). Item ("Age"). ToString)

     'I add the item to the collection of ListViewItem's of ListView1
     ListView1.Items.Add (item)

 Next

Well, we just see a very detailed and comprehensive load data from a DataTable to a ListView. But this code can be optimized much and we can achieve the same result with less code.

A DataTable is a collection of DataRows and through the items in a collection is desirable to use a rather For each one for a controlled variable as in the previous case.

 For Each dr As DataRow In dt.Rows

     "I think the ListViewItem (item)
     Dim item As ListViewItem

     "I think a new instance of item data by passing the name as principal
     item = New ListViewItem (dr ("Name"). ToString)

     'I add the other data to SubItems of item
     item.SubItems.Add (dr ("Name"). ToString)
     item.SubItems.Add (dr ("Age"). ToString)

     'I add the item to the collection of ListViewItem's of ListView1
     ListView1.Items.Add (item)

 Next

As shown in the example we have replaced

 dt.Rows (i). Item ("Name"). ToString

by

 dr ("Name"). ToString

And now we'll see how to optimize all the above code For each contained within a single line using some new concepts such as the use of a string type arrary with the elements of each ListViewItem

 For Each dr As DataRow In dt.Rows

     ListView1.Items.Add (New ListViewItem (New String () {dr ("Name"). ToString, dr ("Name"). ToString, dr ("Age"). ToString}))

 Next

The truth is almost impossible to think of uin project including any ListView, so it's important to catch up with all the virtues of this amazing control. I hope you can apply these concepts in your projects. If you have served, I would like to leave a comment. Thank you!

35 Responses to "Various ways to load a ListView"

  1. Information Bitacoras.com ...

    If you wish, you can click to rate this post Bitacoras.com. Thanks ....

    • bestiaciclope says:

      I'm starting with this from visual basic. Net and I'm still a little lost, but what I saw seemed to me quite clear.

      Thanks

  2. mario pea says:

    hello
    good day just to see if I can help I have a combobox which has already achieved to list data in a database named products which contain these fields "key, description, unit of measure, price," but now I want that when the user selects any option that automatically combobox The Display product description on a label that nesesito code to find the key and I put the description thanks

  3. Elisha says:

    Hi Mario:

    when loading data into a combobox using the DataSource property of the same program you will you can you indicate what the DisplayMember (data to be displayed in the list) and the ValueMember (value to be retrieved by making a selection). You can schedule an event named SelectedIndexChanged with a code like this:

    TextBox1.Text = ComboBox1.SelectedValue

    soon write an article with examples of ComboBox.

    Regards

    Elisha

  4. _chicharo Mario says:

    buendia

    I denuevo
    just to ask why not put an example of how to make a Kardex automated

    you can print and save a document either input or output as well as a report like a hand kardex

    date / article / input / output / existence
    12/1 cream 1 1
    13/1 cream 1 0

    not if I explain because I expect your prompt response

    • Elisha says:

      Mario:

      I apologize for not having access to your request, but the bridar specific solutions is not the goal of this project. What you're asking for is developing a Kardex system, either by way of example or not, but is a system. And the development of the system should do what you ... as I always say: "The best way to learn is by doing."
      And as you can see what you doing all your doubts broadly, to also serve others. For example: how to delete an item from the listview, how to do an UPDATE of a record in the database? or even how to make a report from a DataTable?, etc..
      I hope you understand my position and I hope to carry with your views.

  5. mario pea says:

    ok understood

    well I have a question but I hope not despair but I am a beginner and want to design one for my store sistemita
    to start and do a search on a table for example prductos I find a key from a combo box and the search result dela label I put on such a label from ma put the description and price in another but the target value was from a combobox through this in sql table

    • Elisha says:

      Mario:

      do not quite understand what you want to do, but just published a post showing how to filter the rows in a datagridview , I hope you will serve to guide you. Greetings.

    • goyo says:

      nursed you with your question the truth I think you lost you should take a basic course in programming.
      what you kieres is a system. as stated by the administrator of this site, nothing more examples, not going to teach programming
      also, if kieres for your store, then pay him an engineer to ke dearrolle you a solution to your store.
      ke no release notes, light wool understand
      I am a rookie but I'm not doing that kind of questions.
      best regards

  6. Leonardo said:

    Greetings to apply the same label or textbox?, What I like is that when you select an object from the combobox to be loaded his other label data, an example to see if you understand me better.

    if the table is:
    name, surname, telephone.

    I carry the name field in the combobox and select a name from those, appears on the label, the name and phone number of the selected name.

    thanks in advance

  7. Elisha says:

    Yes, but you only work if you search returns only one record.

  8. Dr. Optimo says:

    Gracia friend ...

    Q I tell you I am very new to vb.net, I'm doing a facu trabajopractico for this year started with vb.net, and this came from ten to see how to use the listview .. I had used the gridview in other years I q .. but this is much better at presenting the data ..

    Thank you. So I followed.

  9. Ari says:

    Good I have a question I have to do a program q q through the digit as the customer leaves your salary in one field d q I have put a combobox and the combobox must deploy various positions as administador, assistant and stuff but that combobox I have it connected to a label q to q if I choose the option combobbox administrator in the label as I pull your salary per hour ... and if I choose another option on the label I pull the hourly wage but depending on the position .. How do I connect the combobox with the label??

    • Elisha says:

      Actually you should not connect. Use the event of the comboBox selectChange to trigger a function to fill the textbox with the corresponding data. To select the correct data to each option you can use a Select Case.

      Select case comboBox1.Text
      case = "Administrative"
      textBoxSueldo.Text = 1200
      case = "Wizard"
      textBoxSueldo.Text = 650
      else case
      textBoxSueldo.Text = 0
      End Select

      Tip: Many times we lose much time looking for the specific solution to a case in point when in fact there. So I recommend you always seek the way to solve the problem by creating a function for this purpose.

      Luck.

  10. Haomaru says:

    Hello, I saw your tutorial and found it great, I followed your steps, but my listview only shows data for this item = New ListViewItem judgment (dr ("RutCliente"). ToString) nothing comes even though more have been added the subitems ... What?
    The data are full, whether under a label, then the dr show the information they contain

    the code is this:

    Dim rows () As DataRow
    rows = dt.Select ("RutCliente LIKE '%" & txtRut.Text & "%'")

    If filas.Length> 0 Then

    For Each dr As DataRow In rows
    Lista.Clear ()
    Dim item As ListViewItem
    item = New ListViewItem (dr ("RutCliente"). ToString)
    item.SubItems.Add (dr ("Company"). ToString)
    item.SubItems.Add (dr ("GiroCliente"). ToString)

    Lista.Items.Add (item)

    Next
    'For Each dr As DataRow In rows
    'Me.ListaClientes.Items.Add (_
    'Dr ("RutCliente"). ToString & "" & _
    'Dr ("Company"). ToString)
    'Next
    End If

    • Elisha says:

      The listview has several display modes and some of them only show the item's information but not the subitems it contains. Try changing the property using ListView1.View = details (you can do it from code or from the properties of the control).

      • Haomaru says:

        I've tried, but simply do not show me Details anything ... the funny thing as I mentioned before, is that if such data by a label, the data is, but for some reason, do not deploy
        The code is this.

        If initiating Then Exit Sub
        Dim rows () As DataRow
        rows = dt.Select ("RutCliente LIKE '%" & txtRut.Text & "%'")
        Me.Lista.Items.Clear ()
        If filas.Length> 0 Then

        For Each dr As DataRow In rows
        Lista.Clear ()
        Dim item As ListViewItem
        item = New ListViewItem (dr ("RutCliente"). ToString)
        lblMuestra.Text = dr ("Company"). ToString
        item.SubItems.Add (Trim (dr ("Company"). ToString))
        item.SubItems.Add (dr ("GiroCliente"). ToString)
        Lista.Items.Add (item)
        Next
        End If

  11. Carlos Humberto Arce says:

    eliseo well you're a good element in these forums ....

  12. Dano says:

    Hello, good contributions by all, I have a question about the listview, what happens is all the examples I've seen about filling a listview data are made using tables or ODBC databases or sqlserver. I wonder if there are ways to fill a listview with textbox (which you type in the textbox is added to a listview of N columns) and fill a listview from a mysql database because that I'm driving.

    I hope your answer greetings ...

    • Elisha says:

      You can fill a listview with whatever you want ... it is true that all the examples are used to take data from a database, but if you look the code, the part where you fill the listview is crafted. The ListView control does not have a DataSource property and DataBinding as other controls.
      I make you email:
      Suppose I want to populate a listview with three columns (name, surname, telephone) and data I have them in three variables or controls textBos or whatever you want. And suppose that these variables are called vName, vApellido and vTelefono.
      With this one line I can fill the three columns of the listview:
      ListView1.Items.Add (New ListViewItem (New String () {vName, vApellido, vTelefono}))
      Try it is very easy. Luck

  13. ANA says:

    HELLO I'M GIVING I'M BEGINNING TO A PROJECT IS GOOD IF YOU WANT TO KNOW HOW TO DO MANAGEMENT clicking on a ListBox and a ComboBox (SELECT A NAME TO ANY OF EACH) TO COME TO A FORM CONTAINING THE DETAILS OF THE PERSON THAT SELECTION

    EXAMPLE

    I HAVE lisbox
    FIRST
    SECOND
    THIRD

    I HAVE COMBOBOX
    A
    B
    C

    THAT TO ME SELECCONAR FORM WITH OTHER DATA THAT URGENT SELECCON FAVORRRRRRRRRRRRRRR

  14. Edu said:

    hi all would like to know if I can to yudar with vb and mysql, I connected using the connector achieve net.6.2.2 but I want to do the following.
    on my form I have 3 textbox, number, name, surname, and a button. I just want to enter the code and to click on the button to show me the full name for the code in which are stored in the database

  15. Edu said:

    hi all would like to know if I can to yudar with vb and mysql, I connected using the connector achieve net.6.2.2 but I want to do the following.
    on my form I have 3 textbox, number, name, surname, and a button. I just want to enter the code and to click on the button to show me the full name for the code in which are stored in the database

    I would appreciate to help ..........

  16. Milena says:

    HELLO please can someone help me make a logo when starting a project in vb.net.

    thank you very much

  17. machado hector says:

    Hi I need someone to tell me how to load a listview text exemplify two texbox ... by id, name ...
    I can load it from the database ..
    but this case is to add a button!

  18. karina says:

    Hello greetings to all.
    Nesecito aid with this, I am doing a basic bisual for inclusion in 6.0. and saves all the data but my prof wants to print the registration form. if anyone knows how to do it agrasco help me. not really how.

  19. Joel says:

    thanks for the tutorials helped me a lot, I thought the listview was simpler in functionality.

  20. Itachisensei says:

    I am a beginner in visual basic it and want to learn to use this program and want to know how to connect a listview to a database as acces2003. I would appreciate your prompt response with all due respect dismissal of you Thanks!

  21. Maristin says:

    How about I'm new to asp.net

    Someone could help me with my problem
    I have a GridView loading data from the DB.

    But what I do is that when you select an object that is opaque grid this information and I throw in a DetailsView the selected object in front of the GridView.
    Anyone know how to do that???

  22. gabiota says:

    hello I need to register progracion buying a home in a lisview several and have a stock then when supplementing the stokc that no longer allows me to record and show me a sms saying there is no. I need that programming porfa ayudenme

  23. gabiota says:

    and go around a lisviw and high quantities of such total wages of all recorded at FNAL in a text box show me the total wages that add ayudenme registered xfa I need this programming

    • Elisha says:

      assuming you have a textobox1 ListView1 below and this listview has two columns and import the code description would be something like this:

      Total dim as doouble = 0
      For Each item As ListViewItem in listview1.items
      total + = item.subitems (1). text
      next
      TextBox1.Text = total

      this will have to guide, is the concept that must adapt to your system, the code I have not tested due to time, but I'm sure it should work fine.

      Luck

  24. Airbit says:

    Hi, thanks for the info, were you looking to learn about the control!

Comment