如何从数据库填充listview? [英] How to populate listview from database?

查看:56
本文介绍了如何从数据库填充listview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从数据库中获取一行数据并将其添加到我的列表视图中?

我想在数据库中的同一行中添加来自数据库中不同表的名称",年龄"和性别",以便它们按顺序显示在我的列表视图的标题列中?


任何建议将不胜感激或更好的方法来实现这一目标.


先感谢您!! :)

How can I take a row of data from a database and add it to my listview?

I am looking to add lets say "Name" "age" and "sex" from different tables on my database all in the same row so that it will show up in my listview in that order in my header columns?


any advise would be greatly appreciated or a better method of how to accomplish this.


thank you in advance!! :)

推荐答案

以下代码假定您已经在使用ADODB代码,并已连接到数据库.


首先,必须将ListView控件添加到项目中.在Microsoft Windows公共控件6.0(SPx)中找到ListView控件(其中x =您的VB Service Pack编号).若要添加这些控件,请从VB IDE中单击项目/组件".找到上述控件,然后选中对应的复选框,然后单击确定".

将ListView控件添加到您的窗体,并将其绘制为所需的尺寸.

接下来,右键单击ListView,然后单击属性".

ListView控件具有4种不同的查看"可能性:
1. lvwIcon
2. lvwSmallIcon
3. lvwList
4. lvwReport

本示例将使用lvwReport.更改视图以反映这一点.

接下来,单击ColumnHeaders选项卡.

此示例将创建一个包含以下内容的员工信息记录集:
名,姓,出生日期,员工编号和雇用日期

用这些标题创建列标题.

我们将使用的数据库字段是:
FName,LName,DOB,EmpNumb,DOH,表的名称为tblEmployeeInfo

我们将使用的数据库字段是:

FName,LName,DOB,EmpNumb,DOH,表的名称为tblEmployeeInfo

Dim strSQL As String''我们的查询字符串
Dim oRS as ADODB.Recordset''我们的记录集对象
Dim lvwItem As ListItem''这是直接引用ListView控件所必需的
设置oRS = New ADODB.Recordset

''根据需要更改此SQL
strSQL =从tblemployeeinfo中选择SELECT FName,LName,DOB,EmpNumb,DOH"
''更改oConn以反映您在程序中使用的Connection对象
oRs.Open strSQL,oConn,adOpenForwardOnly,adLockReadOnly

''加载列表视图
不做oRS.EOF
设置lvwItem = ListView1.ListItems.Add(,,oRS.Fields.Item("FName").Value)
lvwItem.SubItems(1)= oRS.Fields.Item("LName").Value
''根据需要更改日期格式
lvwItem.SubItems(2)= Format(oRS.Fields.Item("DOB").Value,"mm/dd/yyyy")
lvwItem.SubItems(3)= oRS.Fields.Item("EmpNumb").Value
''根据需要更改日期格式
lvwItem.SubItems(4)= Format(oRS.Fields.Item("DOH").Value,"mm/dd/yyyy")
oRS.MoveNext
循环
oRS.Close
设置oRS = Nothing
The following code assumes you are already using ADODB code, and have a connection to the database.


First, you must add a ListView control to your project. The ListView control is found in Microsoft Windows Common Controls 6.0 (SPx) (where x = your VB service pack number). To add these controls, click Project/Components from the VB IDE. Locate the above mentioned controls and check the cooresponding checkbox and click OK.

Add a ListView control to your form, and draw it out to the desired dimensions.

Next, right click on the ListView and click Properties.

The ListView control has 4 different "View" possibilities:
1. lvwIcon
2. lvwSmallIcon
3. lvwList
4. lvwReport

This example will be using lvwReport. Change the view to reflect this.

Next, click on the ColumnHeaders tab.

This example will be creating a recordset of employee information containing:
First Name, Last Name, Date Of Birth, Employee Number and Date Of Hire

Create column headers with these titles.

The database fields we will be using are:
FName, LName, DOB, EmpNumb, DOH and the name of the table is tblEmployeeInfo

The database fields we will be using are:

FName, LName, DOB, EmpNumb, DOH and the name of the table is tblEmployeeInfo

Dim strSQL As String ''our query string
Dim oRS as ADODB.Recordset ''our recordset object
Dim lvwItem As ListItem ''this is necessary to directly reference the ListView control
Set oRS = New ADODB.Recordset

''change this SQL as appropriate for your needs
strSQL = "SELECT FName, LName, DOB, EmpNumb, DOH FROM tblemployeeinfo "
''change oConn to reflect the Connection object you are using in your program
oRs.Open strSQL, oConn, adOpenForwardOnly, adLockReadOnly

''load the listview
Do While Not oRS.EOF
Set lvwItem = ListView1.ListItems.Add(, , oRS.Fields.Item("FName").Value)
lvwItem.SubItems(1) = oRS.Fields.Item("LName").Value
''change the date format as required
lvwItem.SubItems(2) = Format(oRS.Fields.Item("DOB").Value, "mm/dd/yyyy")
lvwItem.SubItems(3) = oRS.Fields.Item("EmpNumb").Value
''change the date format as required
lvwItem.SubItems(4) = Format(oRS.Fields.Item("DOH").Value, "mm/dd/yyyy")
oRS.MoveNext
Loop
oRS.Close
Set oRS = Nothing


这篇关于如何从数据库填充listview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆