ListView DataReader
Imports System.Data.SqlClient Imports System.Windows.Forms <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ Partial Class List Inherits System.Windows.Forms.Form <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.cmdClear = New System.Windows.Forms.Button Me.cmdFill = New System.Windows.Forms.Button Me.lvOrders = New System.Windows.Forms.ListView Me.SuspendLayout() ' Me.cmdClear.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.cmdClear.Location = New System.Drawing.Point(206, 231) Me.cmdClear.Size = New System.Drawing.Size(80, 24) Me.cmdClear.TabIndex = 5 Me.cmdClear.Text = "Clear List" ' Me.cmdFill.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles) Me.cmdFill.Location = New System.Drawing.Point(10, 231) Me.cmdFill.Size = New System.Drawing.Size(80, 24) Me.cmdFill.TabIndex = 4 Me.cmdFill.Text = "Fill List" ' Me.lvOrders.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lvOrders.Location = New System.Drawing.Point(6, 11) Me.lvOrders.Size = New System.Drawing.Size(280, 212) Me.lvOrders.TabIndex = 3 Me.lvOrders.UseCompatibleStateImageBehavior = False Me.lvOrders.View = System.Windows.Forms.View.Details ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.cmdClear) Me.Controls.Add(Me.cmdFill) Me.Controls.Add(Me.lvOrders) Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.ResumeLayout(False) End Sub Friend WithEvents cmdClear As System.Windows.Forms.Button Friend WithEvents cmdFill As System.Windows.Forms.Button Friend WithEvents lvOrders As System.Windows.Forms.ListView Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click lvOrders.Clear() End Sub Private Sub cmdFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFill.Click Dim Connect As String = "Data Source=localhost;Integrated Security=True;Initial Catalog=Northwind" Dim con As New SqlConnection(Connect) Dim SQLString As String = "SELECT * FROM Orders" Dim cmd As New SqlCommand(SQLString, con) con.Open() Dim reader As SqlDataReader = cmd.ExecuteReader() Dim i As Integer For i = 0 To reader.FieldCount - 1 lvOrders.Columns.Add("Column " & (i + 1).ToString, 100, _ HorizontalAlignment.Left) Next Do While (reader.Read()) Dim NewItem As New ListViewItem() NewItem.Text = reader(0) For i = 1 To reader.FieldCount - 1 If reader(i) Is DBNull.Value Then NewItem.SubItems.Add("") Else NewItem.SubItems.Add(reader(i).ToString) End If Next i lvOrders.Items.Add(NewItem) Loop Dim Table As DataTable = reader.GetSchemaTable() For j As Integer = 0 To Table.Rows.Count - 1 lvOrders.Columns(j).Text = Table.Rows(j)("ColumnName") Next reader.Close() con.Close() End Sub End Class