如何将数据库里的数据添加到listview中

如何将数据库里的数据添加到listview中,第1张

有两种办法 一种是自动绑定 还有一种手动绑定 Private Sub Form_Load() On Error GoTo ErrProceed Dim ObjConnTmp As New ADODB.Connection, DataSource As String DataSource = "数据库名" ObjConnTmp.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0Data Source=" &DataSource &"" ObjConnTmp.Open Dim ObjTmpRs As New ADODB.Recordset Set ObjTmpRs.ActiveConnection = ObjConnTmp ObjTmpRs.CursorType = adOpenDynamic ObjTmpRs.LockType = adLockOptimistic '设置游标类型 StrTmpSQL = "Select * from 数据库表名" ObjTmpRs.Open StrTmpSQL FrmMain.ListView1.View = lvwReport FrmMain.ListView1.ColumnHeaders.Clear Call ModListView.SubListViewHeader(FrmMain.ListView1, ObjRsTmp) '加标题 FrmMain.ListView1.ListItems.Clear Call ModListView.SubListViewListItem(FrmMain.ListView1, ObjRsTmp, "End") '加内容 ObjRsTmp.Close: Set ObjRsTmp = Nothing ObjConnTmp.Close: Set ObjConnTmp = Nothing Exit Sub ErrProceed: MsgBox "程序发生错误,原因如下:" &vbCrLf &Err.Description End Sub Public Sub SubListViewHeader(ByVal ListViewName As ListView, ByVal ObjRs As ADODB.Recordset) '功能:将相应的记录的 标题加到ListView中去 Dim clmX As ColumnHeader '设置显示方式 'ListViewName.View = lvwReport '要想显示出网格,请在属性/通用/网格行 设置 nCounter = ObjRs.Fields.Count - 1 ' Dim nWidthGroup() As Integer ' ReDim Preserve nWidthGroup(nCounter) Dim nWidthGroup(8) As Integer k = 100 nWidthGroup(0) = k * 6 '[编号] nWidthGroup(1) = k * 30 '[名称] nWidthGroup(2) = k * 11 '[大小] nWidthGroup(3) = k * 8 '[类型] nWidthGroup(4) = k * 20 '[修改日期] nWidthGroup(5) = k * 20 '[记录日期] nWidthGroup(6) = k * 0 '[文件] For i = 0 To nCounter nWidth = nWidthGroup(i) Set clmX = ListViewName.ColumnHeaders.Add(, , ObjRs.Fields(i).Name, nWidth) Select Case ObjRs.Fields(i).Name Case "类型" clmX.Alignment = lvwColumnCenter Case "大小" clmX.Alignment = lvwColumnRight End Select Next End Sub Public Sub SubListViewListItem(ByVal ListViewName As ListView, ByVal ObjRs As ADODB.Recordset, ByVal StrLocate As String) '功能:将相应的记录的 内容加到ListView中去 'StrLocate 用来定位新加记录在原ListView中的相对位置 Begin,开头 End 结尾 Dim itmX As ListItem '设置显示方式 'ListViewName.View = lvwReport '要想显示出网格,请在属性/通用/网格行 设置 '加内容 If ObjRs.EOF And ObjRs.BOF Then Exit Sub ObjRs.MoveFirst Do Until ObjRs.EOF If StrLocate = "End" Then Set itmX = ListViewName.ListItems.Add(, , ObjRs(0)) Else Set itmX = ListViewName.ListItems.Add(1, , ObjRs(0)) End If '向列表中加入数据 For i = 1 To ObjRs.Fields.Count - 1 StrSubItem = FunNullConvert(ObjRs(i)) Select Case ObjRs.Fields(i).Name Case "大小" '如果是文件大小,则格式化其显示方式为加上千分位 StrSubItem = Format(CLng(StrSubItem), "#,###") Case "修改日期", "记录日期" If StrSubItem <>"" Then StrSubItem = FormatDateTime(CDate(StrSubItem), vbGeneralDate) End Select itmX.SubItems(i) = CStr(StrSubItem) Next i ObjRs.MoveNext Loop End Sub

提供思路。具体问题自己解决。。。

首先 初始化ListView

this.listView1.Columns.Add("姓名", 60, HorizontalAlignment.Right)

this.listView1.Columns.Add("性别", 60, HorizontalAlignment.Right)

this.listView1.Columns.Add("电话", 60, HorizontalAlignment.Right)

this.listView1.Columns.Add("地址", 60, HorizontalAlignment.Right)

然后 添加数据

ListViewItem li = new ListViewItem()

li.SubItems.Add(文本框1的内容 比如 textBox1.Text)

li.SubItems.Add(文本框的内容)

li.SubItems.Add(文本框的内容)//注有几项 就添加几列

this.listView1.Items.Add(li)

如果是数据库添加,还相应的去修改添加数据库i,这个自己搞定

接下来 重点帮你弄下 选中一行 显示在文本框中的代码:

private void listView1_SelectedIndexChanged(object sender, EventArgs e)

{

if (listView1.SelectedIndices.Count >0)

{

int index = listView1.SelectedIndices[0]//获取当前选中项的内容索引

//开始显示 文本框 对应 自己看着对应就可以了

TextBox1.text = this.listView1.Items[index].SubItems[0].Text//选中当前项的第一列

TextBox2.text = this.listView1.Items[index].SubItems[1].Text//选中当前项的第二列

TextBox3.text = this.listView1.Items[index].SubItems[2].Text//选中当前项的第三列

TextBox4.text = this.listView1.Items[index].SubItems[3].Text//选中当前项的第四列

TextBox5.text = this.listView1.Items[index].SubItems[4].Text//选中当前项的第五列

TextBox6.text = this.listView1.Items[index].SubItems[5].Text//选中当前项的第六列

TextBox7.text = this.listView1.Items[index].SubItems[6].Text//选中当前项的第七列

TextBox8.text = this.listView1.Items[index].SubItems[7].Text//选中当前项的第八列

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9996908.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存