.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Display records from database to ListView

Posted By:satyapriyanayak       Posted Date: May 02, 2013    Points: 40    Category: Visual Studio    URL: http://www.dotnetspark.com  

Display records from database to ListView
 

Code Example
Imports System.Data

Imports System.Data.OleDb

Public Class Form1

    Dim ConnectionString As String = System.Configuration.ConfigurationSettings.AppSettings("dsn")

    Dim con As OleDbConnection = New OleDbConnection(ConnectionString)

    Dim com As OleDbCommand

    Dim oledbda As OleDbDataAdapter

    Dim ds As DataSet

    Dim dt As DataTable

    Dim str As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        con.Open()

        str = "select * from student"

        com = New OleDbCommand(str, con)

        oledbda = New OleDbDataAdapter(com)

        ds = New DataSet

        oledbda.Fill(ds, "student")

        ListView1.Clear()

        dt = ds.Tables("student")

        Dim i As Integer

        For i = 0 To dt.Rows.Count - 1

            ListView1.Items.Add(dt.Rows(i).ItemArray(0))

            ListView1.Items(i).SubItems.Add(dt.Rows(i).ItemArray(1))

            ListView1.Items(i).SubItems.Add(dt.Rows(i).ItemArray(2))

            ListView1.Items(i).SubItems.Add(dt.Rows(i).ItemArray(3))

            ListView1.Items(i).SubItems.Add(dt.Rows(i).ItemArray(4))

        Next

        con.Close()

 

        ListView1.Columns.Add("Id", 60, HorizontalAlignment.Center)

        ListView1.Columns.Add("Name", 80, HorizontalAlignment.Center)

        ListView1.Columns.Add("Marks", 90, HorizontalAlignment.Center)

        ListView1.Columns.Add("Address", 90, HorizontalAlignment.Center)

        ListView1.Columns.Add("Year", 80, HorizontalAlignment.Center)

        ListView1.View = View.Details

        ListView1.GridLines = True

        ListView1.BackColor = Color.Aqua

        ListView1.ForeColor = Color.Blue

    End Sub

End Class

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend