Tag Archives: ListView

How to fill up a ListView from database in VB.NET using a DataTable

You can easily use the following code to fill up your listview control from the data in a datatable using VB.NET

 

Supply the name of your listview and the datatable to this procedure and it will fill it up your list view.

If you wish to display the ongoing process then you can do that as well. Just create a label and then uncomment the following text in the procedure. Create a label and call it myStatusLabel.

‘ myStatusLabel.text = “Displaying Record ” & i + 1 & “/” & iRowCount

 

 

Sub FillListViewByTable(ByVal listView As ListView, ByVal dt As DataTable)
        Dim ii As Integer
        Dim iColCount As Integer = dt.Columns.Count
        Dim iRowCount As Integer = dt.Rows.Count
        listView.Clear()
        For ii = 0 To iColCount – 1
            listView.Columns.Add(dt.Columns(ii).ColumnName, 250, HorizontalAlignment.Left)
          
        Next
   
        Dim i As Integer
        Dim x As Integer
        For i = 0 To iRowCount – 1  ‘ for each row in the table
            Dim lst As New ListViewItem
            lst.Text = dt.Rows(i).Item(0)
            lst.UseItemStyleForSubItems = True
            For x = 1 To iColCount – 1 ‘ for each column in the row of the table
                Application.DoEvents()
                lst.SubItems.Add(SNZ(dt.Rows(i).Item(x)))
             
            Next

               Application.DoEvents()
           ‘    If you want to display the progress then you can do it here
           ‘ myStatusLabel.text = “Displaying Record ” & i + 1 & “/” & iRowCount
            listView.Items.Add(lst)
            lst = Nothing
        Next
    
    End Sub