Bind Listview from dataset in vb.net

 
The following code snippet can be used for binding the listview control with dataset or datatable. The following example requires a Listview control name Listview1.

Set the View property of the Listview1 to ‘details’. this property will include the columns appearance like datagridview control.
In the following example, i am binding first all columns of the dataset to Listview1 and then all rows. see this:  

    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

As you see, we can use the listview control as alternate of the datagridview control.
Thanks

3 thoughts on “Bind Listview from dataset in vb.net”

  1. This is not binding. Make sure you know what you are talking about before you post an article.

Comments are closed.