有两种办法 一种是自动绑定 还有一种手动绑定 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你可以在第一第二列
添加空数据,string.Empty,我印象中你是必须要给,即使是没有数据也要new一个ListViewItem出来
回答补充:
你不能往某一列中添加数据,你只能添加一行数据,只不过需要你向不想添加数据的列添加空数据罢了
绑定数据库数据到listview中,有两种方式 第一种:使用SqlDateReader public void BindData() { //连接数据库 string strcon="server=.database=studentuid=sapwd=123456"SqlConnection con=new SqlConnection(strcon)con.Open()strin...
评论列表(0条)