[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 19
  • Last Modified:

Using List View Groups

Hi experts,
Having problems with a list view in VB.net and cant get the groups to work.

Coming in is a datatable with Part number, order number, order qauntity.

I am trying to create groups in the listview to group the orders by part number.

 Public Sub PopulateListViewFromDt(ByVal Dt As DataTable)
        Dim Dtunique As New DataTable

'Find the unique part number list
        If Dt.Rows.Count > 0 Then
            Dtunique = Dt.DefaultView.ToTable(True, GroupName)
        End If

'Add a group for each item in the unique part number list
        For Each row As DataRow In Dtunique.Rows
            Dim LVG As New ListViewGroup
            With LVG
                .Name = row(GroupName)
                .Header = row(GroupName)
            End With
            ListViewName.Groups.Add(LVG)
        Next

'Create the list view item for each item in the datatable.
           Dim LVG As ListViewGroup
            Dim LVI As New ListViewItem

' I would rather not do this step....I would like to create the group and assign an item to the group, since the group name is known.
            For Each Group In ListViewName.Groups
                If Group.name = row("PartNumber") Then
                    LVG = Group
                End If
            Next
'******************************

            With LVI
                If row(ValidationCriteria) = True Then
                    .Checked = True
                End If

                For Each Col In Columns
                    If Col <> TopItem Then
                        .SubItems.Add(row(Col).ToString)
                    End If
                Next

                .Name = row("PartNumber").ToString
                .Group = LVG
            End With

            ListViewName.Items.Add(LVI)
        Next

    End Sub

Open in new window


Thanks for the help in advance, I have read many help topics but I must be missing something.

Regards,
K.
0
kerrymr
Asked:
kerrymr
  • 4
  • 2
1 Solution
 
kerrymrAuthor Commented:
I dont understand how that is fundamentally different than what I did.

   .Group = LVG

Open in new window

0
 
BillSoftware EngineerCommented:
not sure what your code is doing. why don't you make it easy on yourself and sort the dataTable. it is a few easy steps. Follow the instructions in this link

https://stackoverflow.com/questions/9107916/sorting-rows-in-a-data-table
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
kerrymrAuthor Commented:
I already filter the datatable to unique records of part number.
Then I use those unique part numbers to create list view groups.
So the question still stands, I created the groups how do I assign items to the groups?

        For Each Item As ListViewItem In ListViewName.Items
            Item.Group = ?????????????????
        Next

Open in new window

0
 
it_saigeDeveloperCommented:
Are you wanting to accomplish something like this:

Form1.vb -
Imports System.ComponentModel
Imports System.Runtime.CompilerServices

Public Class Form1
	ReadOnly parts = (From i In Enumerable.Range(0, 30)
					  Select New With
						{
						  .ID = i,
						  .PartNumber = If(i Mod 5 = 0, 5050, If(i Mod 4 = 0, 4040, If(i Mod 3 = 0, 3030, If(i Mod 2 = 0, 2020, 1010)))),
						  .OrderNumber = $"OrderNumber{i}",
						  .OrderQuantity = i + 1 * 4
						}).ConvertToDataTable()
	Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
		ListView1.PopulateListViewFromDT(parts)
		ListView1.View = View.Details
	End Sub
End Class

Module Extensions
	<Extension()>
	Public Function ConvertToDataTable(Of T)(ByVal source As IEnumerable(Of T), Optional ByVal name As String = Nothing) As DataTable
		Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(T))
		Dim table As DataTable = New DataTable(If(Not String.IsNullOrWhiteSpace(name), name, String.Empty))

		For i As Integer = 0 To properties.Count - 1
			Dim [property] As PropertyDescriptor = properties(i)
			If [property].PropertyType.IsGenericType AndAlso [property].PropertyType.GetGenericTypeDefinition().Equals(GetType(Nullable)) Then
				table.Columns.Add([property].Name, [property].PropertyType.GetGenericArguments()(0))
			Else
				table.Columns.Add([property].Name, [property].PropertyType)
			End If
		Next

		Dim values(properties.Count - 1) As Object
		For Each item As T In source
			For i As Integer = 0 To properties.Count - 1
				values(i) = properties(i).GetValue(item)
			Next
			table.Rows.Add(values)
		Next

		Return table
	End Function

	<Extension()>
	Public Sub PopulateListViewFromDT(target As ListView, source As DataTable)
		If source IsNot Nothing AndAlso source.Rows.Count > 0 Then
			target.Columns.AddRange((From column In source.Columns.Cast(Of DataColumn) Select New ColumnHeader() With {.Name = column.ColumnName, .Text = column.ColumnName, .Width = 100}).ToArray())
			For Each row In source.AsEnumerable().Cast(Of DataRow)
				Dim group = target.Groups.Cast(Of ListViewGroup).Where(Function(x) x.Name = row("PartNumber")).SingleOrDefault()
				If group Is Nothing Then
					group = New ListViewGroup With {.Name = row("PartNumber"), .Header = row("PartNumber")}
					target.Groups.Add(group)
				End If
				target.Items.Add(New ListViewItem((From item In row.ItemArray Select $"{item}").ToArray()) With {.Group = group})
			Next
		End If
	End Sub
End Module

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
	Inherits System.Windows.Forms.Form

	'Form overrides dispose to clean up the component list.
	<System.Diagnostics.DebuggerNonUserCode()> _
	Protected Overrides Sub Dispose(ByVal disposing As Boolean)
		Try
			If disposing AndAlso components IsNot Nothing Then
				components.Dispose()
			End If
		Finally
			MyBase.Dispose(disposing)
		End Try
	End Sub

	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer

	'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.ListView1 = New System.Windows.Forms.ListView()
		Me.SuspendLayout()
		'
		'ListView1
		'
		Me.ListView1.Location = New System.Drawing.Point(13, 13)
		Me.ListView1.Name = "ListView1"
		Me.ListView1.Size = New System.Drawing.Size(655, 236)
		Me.ListView1.TabIndex = 0
		Me.ListView1.UseCompatibleStateImageBehavior = False
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(680, 261)
		Me.Controls.Add(Me.ListView1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)

	End Sub

	Friend WithEvents ListView1 As ListView
End Class

Open in new window

Which produces the following output -Capture.PNG
-saige-
0
 
kerrymrAuthor Commented:
This looks very much like what I'm looking for.  
Give me some time to test and I'll advise if complete or additional questions.  
It does look very promising.

K.
0
 
kerrymrAuthor Commented:
Thank you very much that did indeed do what I needed.
0

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now