通用填充Listview子例程 [英] Generic Fill Listview Subroutine

查看:90
本文介绍了通用填充Listview子例程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我有这段代码用字符串字段填充Listview
有没有办法对字符串和数字字段使用相同的例程?

Hi to all
I have this code to fill Listview with string fields
Is there any way to use the same routine for string and numeric fields????

Do While myData.Read
    lv = New ListViewItem
    strValue = IIf(myData.IsDBNull(0), "", myData.GetValue(0))
    lv.Text = strValue
    For i = 1 To myData.FieldCount() - 1
        If myData.IsDBNull(i) Then
            lv.SubItems.Add("")
        Else
            lv.SubItems.Add(myData.GetString(i))
        End If
    Next i
    Cntr += 1
    lvList.Items.Add(lv)
Loop

推荐答案

尝试以下方法:

Try this:

Dim data As String = ""
For i = 1 To myData.FieldCount() - 1
    data = ""
    if (Not myData.IsDBNull(i) Then
        data = myData(i).ToString()
    lv.SubItems.Add(data)
Next


Public Sub fill_listview(ByVal query As String, ByVal lv As ListView)
        Dim ds As New DataSet
        ds = fill_dataset(query)
        lv.Items.Clear()
        lv.GridLines = True
        lv.FullRowSelect = True
        lv.HideSelection = False
        Dim x, y As Integer
        For x = 0 To ds.Tables(0).Rows.Count - 1
            Dim lv_item As New ListViewItem
            lv_item.Text = ds.Tables(0).Rows(x)(0).ToString()
            For y = 1 To ds.Tables(0).Columns.Count - 1
                lv_item.SubItems.Add(ds.Tables(0).Rows(x)(y).ToString())
            Next
            lv.Items.Add(lv_item)
        Next
    End Sub







Public Function fill_dataset(ByVal query As String) As DataSet
        connect()
        command.Connection = con
        command.CommandText = query
        command.ExecuteNonQuery()
        Dim ds As New DataSet
        adapter.SelectCommand = command
        adapter.Fill(ds)
        con.Close()
        Return ds
    End Function


这篇关于通用填充Listview子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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