Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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????

VB
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
Posted
Updated 11-Apr-11 5:06am
v2

Try this:

VB
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
 
Share this answer
 
v2
VB
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



VB
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900