使用C#怎么把数据库里的数据使用集合读取出来并展示在listView上

使用C#怎么把数据库里的数据使用集合读取出来并展示在listView上,第1张

namespace ShopModel{public class Shops{string shopname double Price int number int allnumber int singlenumber public string Shopname{get { return shopname}set { shopname = value}}public double Price1{get { return Price}set { Price = value}}public int Number{get { return number}set { number = value}} public int Allnumber{get { return allnumber}set { allnumber = value}}public int Singlenumber{get { return singlenumber}set { singlenumber = value}} }}查询所有数据private static IList<Shops>Getnumber(){string sql = "select * from Shop" List<Shops>list = new List<Shops>() using(DataTable dt = DBHelper.GetDataSet(sql)){foreach(DataRow row in dt.Rows ){Shops shop = new Shops() shop.Shopname =(string)row["SpName"] shop.Number =(int)row["SpNumber"] shop.Price1 = Convert.ToDouble( row["SpPrice"]) shop.Allnumber = (int)row["SpAllnumber"] shop.Singlenumber = (int)row["Spsinglenumber"] list.Add(shop) }return list }}

有两种办法 一种是自动绑定 还有一种手动绑定

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

ListViewItem listViewItem = new ListViewItem(number)

listViewItem.SubItems.Add(name)

listViewItem.SubItems.Add(id)

listViewItem.SubItems.Add(address)

listViewItem.SubItems.Add(phone)

listViewItem.SubItems.Add(admission_time)

listViewItem.SubItems.Add(departments)

listViewItem.SubItems.Add(classes)

listViewItem.SubItems.Add(birthday)

listView1.Items.Add(listViewItem)


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存