Fill XML data into ListView and add column to ListView at run time : ListView « GUI « VB.Net






Fill XML data into ListView and add column to ListView at run time

Fill XML data into ListView and add column to ListView at run time
 
Imports System
Imports System.Collections
Imports System.Data

Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Drawing.Drawing2D
Imports System.IO

Public Class MainClass
    
    Shared Sub Main()
        Dim form1 As Form = New Form1

        Application.Run(form1)


    End Sub
End Class




Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Friend WithEvents cmdFillList As System.Windows.Forms.Button
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents optSmallIcon As System.Windows.Forms.RadioButton
    Friend WithEvents optLargeIcon As System.Windows.Forms.RadioButton
    Friend WithEvents optDetails As System.Windows.Forms.RadioButton
    Friend WithEvents optList As System.Windows.Forms.RadioButton
    Friend WithEvents listAuthors As System.Windows.Forms.ListView
    Private components As System.ComponentModel.IContainer

    'Required by the Windows Form Designer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox()
        Me.optLargeIcon = New System.Windows.Forms.RadioButton()
        Me.optList = New System.Windows.Forms.RadioButton()
        Me.optDetails = New System.Windows.Forms.RadioButton()
        Me.optSmallIcon = New System.Windows.Forms.RadioButton()
        Me.listAuthors = New System.Windows.Forms.ListView()
        Me.cmdFillList = New System.Windows.Forms.Button()
        Me.GroupBox1.SuspendLayout()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.optLargeIcon, Me.optList, Me.optDetails, Me.optSmallIcon})
        Me.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.GroupBox1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.GroupBox1.Location = New System.Drawing.Point(276, 12)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(104, 132)
        Me.GroupBox1.TabIndex = 2
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "View"
        '
        'optLargeIcon
        '
        Me.optLargeIcon.Checked = True
        Me.optLargeIcon.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.optLargeIcon.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.optLargeIcon.Location = New System.Drawing.Point(16, 48)
        Me.optLargeIcon.Name = "optLargeIcon"
        Me.optLargeIcon.Size = New System.Drawing.Size(76, 16)
        Me.optLargeIcon.TabIndex = 0
        Me.optLargeIcon.TabStop = True
        Me.optLargeIcon.Text = "LargeIcon"
        '
        'optList
        '
        Me.optList.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.optList.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.optList.Location = New System.Drawing.Point(16, 96)
        Me.optList.Name = "optList"
        Me.optList.Size = New System.Drawing.Size(56, 16)
        Me.optList.TabIndex = 0
        Me.optList.Text = "List"
        '
        'optDetails
        '
        Me.optDetails.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.optDetails.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.optDetails.Location = New System.Drawing.Point(16, 72)
        Me.optDetails.Name = "optDetails"
        Me.optDetails.Size = New System.Drawing.Size(72, 16)
        Me.optDetails.TabIndex = 0
        Me.optDetails.Text = "Details"
        '
        'optSmallIcon
        '
        Me.optSmallIcon.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.optSmallIcon.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.optSmallIcon.Location = New System.Drawing.Point(16, 24)
        Me.optSmallIcon.Name = "optSmallIcon"
        Me.optSmallIcon.Size = New System.Drawing.Size(72, 16)
        Me.optSmallIcon.TabIndex = 0
        Me.optSmallIcon.Text = "SmallIcon"
        '
        'listAuthors
        '
        Me.listAuthors.Activation = System.Windows.Forms.ItemActivation.OneClick
        Me.listAuthors.AllowColumnReorder = True
        Me.listAuthors.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.listAuthors.GridLines = True
        Me.listAuthors.HoverSelection = True
        Me.listAuthors.Location = New System.Drawing.Point(8, 8)
        Me.listAuthors.Name = "listAuthors"
        Me.listAuthors.Size = New System.Drawing.Size(260, 332)
        Me.listAuthors.Sorting = System.Windows.Forms.SortOrder.Ascending
        Me.listAuthors.TabIndex = 0
        '
        'cmdFillList
        '
        Me.cmdFillList.Anchor = (System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right)
        Me.cmdFillList.FlatStyle = System.Windows.Forms.FlatStyle.System
        Me.cmdFillList.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.cmdFillList.Location = New System.Drawing.Point(276, 152)
        Me.cmdFillList.Name = "cmdFillList"
        Me.cmdFillList.Size = New System.Drawing.Size(104, 24)
        Me.cmdFillList.TabIndex = 1
        Me.cmdFillList.Text = "Fill List"
        '
        'ListViewExample
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
        Me.ClientSize = New System.Drawing.Size(388, 349)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.GroupBox1, Me.cmdFillList, Me.listAuthors})
        Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Name = "ListViewExample"
        Me.Text = "ListView Example"
        Me.GroupBox1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region


    Private Sub cmdFillList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFillList.Click

        listAuthors.BeginUpdate()

        Dim dt As DataTable = StoreDB.GetProducts()
        Dim dr As DataRow

        For Each dr In dt.Rows
            Dim listItem As New ListViewItem(dr("book").ToString)
            listItem.ImageIndex = 0

            listItem.SubItems.Add(dr("title"))
            listAuthors.Items.Add(listItem)
        Next

        listAuthors.Columns.Add("title", 100, HorizontalAlignment.Left)

        listAuthors.EndUpdate()
    End Sub

    Private Sub NewView(ByVal sender As System.Object, ByVal e As System.EventArgs) _
 Handles optSmallIcon.CheckedChanged, optDetails.CheckedChanged, _
 optLargeIcon.CheckedChanged, optList.CheckedChanged

        listAuthors.View = CType(sender, Control).Tag
        Me.Text = "Using View: " & listAuthors.View.ToString

    End Sub


    Private Sub ListViewExample_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        optLargeIcon.Tag = View.LargeIcon
        optSmallIcon.Tag = View.SmallIcon
        optDetails.Tag = View.Details
        optList.Tag = View.List
        Call cmdFillList_Click(Nothing, Nothing)
    End Sub


    Private Sub listAuthors_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles listAuthors.ColumnClick
        listAuthors.ListViewItemSorter = New CompareListViewItems(e.Column)
        listAuthors.Sort()
    End Sub
End Class

Public Class CompareListViewItems
    Implements IComparer

    Public ReadOnly Column As Integer

    Public Sub New(ByVal columnIndex As Integer)
        Column = columnIndex
    End Sub

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim ListX As ListViewItem = CType(x, ListViewItem)
        Dim ListY As ListViewItem = CType(y, ListViewItem)

        If ListX.SubItems(Column).Text > ListY.SubItems(Column).Text Then
            Return 1
        ElseIf ListX.SubItems(Column).Text = ListY.SubItems(Column).Text Then
            Return 0
        Else
            Return -1
        End If

    End Function
End Class

Public Class StoreDB

    Public Shared Function GetProducts() As DataTable
        Dim dsStore As New DataSet()

        dsStore.ReadXmlSchema("book.xdr")
        dsStore.ReadXml("book.xml")

        Return dsStore.Tables("books")
    End Function


End Class
           
         
  








Related examples in the same category

1.ListView Custom sortListView Custom sort
2.Build ListView at run time: update title column textBuild ListView at run time: update title column text
3.Construct ListView at run time: build structure and insert recordsConstruct ListView at run time: build structure and insert records
4.Large Icon and small Icon for ListView
5.Add Customized Item to ListViewAdd Customized Item to ListView
6.Add Selection Listener to the ListViewAdd Selection Listener to the ListView
7.Add Items to ListViewAdd Items to ListView
8.Displaying directories and their contents in ListViewDisplaying directories and their contents in ListView
9.Set ListView header and fill dataSet ListView header and fill data
10.Drag and Drop Explorer
11.ListView DataReader