2022年7月19日 星期二

Bind Listview from dataset

 Dim constr As String = "Put your connection string here"

        Dim ds As New DataSet

        Dim con As New SqlClient.SqlConnection(constr)

        con.Open()

        Dim sqladap As New SqlClient.SqlDataAdapter("select * from tbl_Employee", con)

        sqladap.Fill(ds)

 

        For i As Integer = 0 To ds.Tables(0).Columns.Count - 1

            ListView1.Columns.Add(ds.Tables(0).Columns(i).ToString())

        Next

        For i As Integer = 0 To ds.Tables(0).Rows.Count - 1

            Dim listRow As New ListViewItem

            listRow.Text = ds.Tables(0).Rows(i)(0).ToString()

            For j As Integer = 1 To ds.Tables(0).Columns.Count - 1

                listRow.SubItems.Add(ds.Tables(0).Rows(i)(j).ToString())

            Next

            ListView1.Items.Add(listRow)

        Next

沒有留言:

張貼留言