none
DataTable binding Listview RRS feed

Answers

  • Hi,

    I hope the code can help you.

    Public Class Form1
        Dim t1 As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With t1
                .Columns.Add("One")
                .Columns.Add("Two")
                .Columns.Add("Three")
                .Columns.Add("Four")
                .Columns.Add("Five")
                .Columns.Add("Six")
                .Rows.Add("A", "B", "C", "D", "E", "F")
                .Rows.Add("G", "H", "I", "J", "K", "L")
                .Rows.Add("M", "N", "O", "P", "Q", "R")
                .Rows.Add("S", "T", "U", "V", "W", "X")
                ShowDataInListView(t1, ListView1)
            End With
    
        End Sub
        Public Sub ShowDataInListView(ByVal dt As DataTable, ByVal lv As ListView)
            lv.Clear()
            lv.AllowColumnReorder = True
            lv.GridLines = True
            lv.FullRowSelect = True
            lv.View = View.Details
            lv.Scrollable = True
            lv.MultiSelect = False
            lv.HeaderStyle = ColumnHeaderStyle.Clickable
            lv.View = View.Details
            Dim RowCount, ColumnCount As Integer
            Dim dr As DataRow = Nothing
            If dt Is Nothing Then Return
            RowCount = dt.Rows.Count
            ColumnCount = dt.Columns.Count
    
            For i As Integer = 0 To ColumnCount - 1
                lv.Columns.Add(dt.Columns(i).Caption.Trim().ToString, CInt(lv.Width / ColumnCount))
            Next
    
            If RowCount = 0 Then Return
    
            For i As Integer = 0 To RowCount - 1
                dr = dt.Rows(i)
                lv.Items.Add(dr(0).ToString())
    
                For j As Integer = 1 To ColumnCount - 1
                    lv.Items(i).SubItems.Add(dr(j).ToString())
                Next
            Next
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CLG123 Thursday, September 20, 2018 12:53 PM
    Thursday, September 20, 2018 5:35 AM

All replies

  • Hi,

    I hope the code can help you.

    Public Class Form1
        Dim t1 As New DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With t1
                .Columns.Add("One")
                .Columns.Add("Two")
                .Columns.Add("Three")
                .Columns.Add("Four")
                .Columns.Add("Five")
                .Columns.Add("Six")
                .Rows.Add("A", "B", "C", "D", "E", "F")
                .Rows.Add("G", "H", "I", "J", "K", "L")
                .Rows.Add("M", "N", "O", "P", "Q", "R")
                .Rows.Add("S", "T", "U", "V", "W", "X")
                ShowDataInListView(t1, ListView1)
            End With
    
        End Sub
        Public Sub ShowDataInListView(ByVal dt As DataTable, ByVal lv As ListView)
            lv.Clear()
            lv.AllowColumnReorder = True
            lv.GridLines = True
            lv.FullRowSelect = True
            lv.View = View.Details
            lv.Scrollable = True
            lv.MultiSelect = False
            lv.HeaderStyle = ColumnHeaderStyle.Clickable
            lv.View = View.Details
            Dim RowCount, ColumnCount As Integer
            Dim dr As DataRow = Nothing
            If dt Is Nothing Then Return
            RowCount = dt.Rows.Count
            ColumnCount = dt.Columns.Count
    
            For i As Integer = 0 To ColumnCount - 1
                lv.Columns.Add(dt.Columns(i).Caption.Trim().ToString, CInt(lv.Width / ColumnCount))
            Next
    
            If RowCount = 0 Then Return
    
            For i As Integer = 0 To RowCount - 1
                dr = dt.Rows(i)
                lv.Items.Add(dr(0).ToString())
    
                For j As Integer = 1 To ColumnCount - 1
                    lv.Items(i).SubItems.Add(dr(j).ToString())
                Next
            Next
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by CLG123 Thursday, September 20, 2018 12:53 PM
    Thursday, September 20, 2018 5:35 AM
  • Hi Alex,

    Thanks

    Thursday, September 20, 2018 12:54 PM
  • Not a valid answer...

    You cannot bind a listview to a datatable.

    Answer has no binding methods. 


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Thursday, September 20, 2018 1:50 PM