Populating a Listview, Data from database table

Were Done on inserting and searching a record another functionality that we want to do is to show all of the list that were inserted from the database table, there several control the you can use Datagrid, listbox but for me the most complex is  a listview. for you to  start using a listview you need to add this component press ctrl+t on your keyboard to show components be sure that you check the following components



if you  add this components new controls will be added on your control box




just double click the listview on your toolbox   right click the listview that had been created when you double click it then click on the properties then follow the following properties of the listview
 


on the view I selected lvwreport this indicates that we want to create a lisview that is arranged by rows and columns, then  labelEdit for lvwmanual so that when you click the selection you will not have the power to change the value of the first column of the selection, Hide selection is unchecked because we want to see the current selection, Fullrowselect if this property is unchecked only the first column will be highlighted  and lastly gridlines so that there will be a separation for every column and Row

after setting this properties click on the column header and add 4 columns PID,Product Name, Stocks and Price



 Were done with the design Lets do the Coding :)

For the coding we need to use a recordset and listitem

Click on the empty space of your  Form if you will double click it you will see a function that will be created
 "Form Load" initially we have a code on the form load it is where we call a function called init_con.

init_con
Dim lv As ListItem
' declaration of array that will be needed to for inserting the records to the listview

Dim rstload As New ADODB.Recordset
'declaration of recordset named rstload
rstload.Open "Select * from tbl_Products", con, 3, 3
'selecting the records on the tbl_products

rstload.MoveFirst
'it move the cursor to the first row of record on that table
Do While Not rstload.EOF
'a keyword for a looping statement it is used to read all the records on th table
    Set lv = Me.lvproducts.ListItems.Add(, , rstload.Fields("fld_PID"))
    ' the first column will be populated with the value of PID
    lv.SubItems(1) = rstload.Fields("fld_Productname")
    ' the 2nd column will be populated with the value of product name
   
    lv.SubItems(2) = rstload.Fields("fld_Price")
    ' the 3rd column will be populated with the value of fld_price
   
    lv.SubItems(3) = rstload.Fields("fld_Stocks")
    ' the fourth column will be populated with the value of fld_stocks
   
    'dont be confused with the numbering takenote that the numbering starts with 0,1,2,3
   
rstload.MoveNext
    'moves to the next record on the table until it reaches the end
Loop

takenote that the name of the listview is lvproducts

when you correctly applied this functionality your form looks like this when you run it





happy coding



Walang komento:

Mag-post ng isang Komento