ListView Groups doesn't change the name.

Ennio

Member
Joined
Mar 5, 2006
Messages
23
Programming Experience
Beginner
I want to add groups to my listview but for some reason when I create the ListVeiw group it doesn't change the name, it always display DEFAULT as the name of the group.


VB.NET:
[SIZE=2][COLOR=#008000]'Create groups
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] lsvGroup [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ListViewGroup([/SIZE][SIZE=2][COLOR=#800000]"Project Name"[/COLOR][/SIZE][SIZE=2], HorizontalAlignment.Left)[/SIZE]
[SIZE=2]
lsvGroup.Name = [/SIZE][SIZE=2][COLOR=#800000]"PROJNAME"
[/COLOR][/SIZE][SIZE=2]lsvReport.Groups.Add(lsvGroup)
[/SIZE]
 
Set the .Title property... the name is just that... the name of the grouping, but it's not what gets displayed. For that set the Title property of the group.

-tg
 
My bad... it's .Header that you want.

-tg
 
I must be doing something wrong and I cannot see I still get the default name

here is my code

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] frmTrackTable_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load
[/SIZE][SIZE=2][COLOR=#008000]'Create the Columns name
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]With[/COLOR][/SIZE][SIZE=2] lsvReport
.Columns.Add([/SIZE][SIZE=2][COLOR=#800000]"Project Name"[/COLOR][/SIZE][SIZE=2], 150, HorizontalAlignment.Left)
.Columns.Add([/SIZE][SIZE=2][COLOR=#800000]"Date"[/COLOR][/SIZE][SIZE=2], 80, HorizontalAlignment.Left)
.Columns.Add([/SIZE][SIZE=2][COLOR=#800000]"Time In"[/COLOR][/SIZE][SIZE=2], 100, HorizontalAlignment.Left)
.Columns.Add([/SIZE][SIZE=2][COLOR=#800000]"Time Out"[/COLOR][/SIZE][SIZE=2], 100, HorizontalAlignment.Left)
.Columns.Add([/SIZE][SIZE=2][COLOR=#800000]"Total hours worked"[/COLOR][/SIZE][SIZE=2], 120, HorizontalAlignment.Left)
.AllowColumnReorder = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2].View = View.Details
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]With
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Create groups
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] lsvGroup [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ListViewGroup
lsvGroup.Name = [/SIZE][SIZE=2][COLOR=#800000]"PROJNAME"
[/COLOR][/SIZE][SIZE=2]lsvGroup.Header = [/SIZE][SIZE=2][COLOR=#800000]"Project Name"
[/COLOR][/SIZE][SIZE=2]lsvReport.Groups.Add(lsvGroup)
[/SIZE][SIZE=2][COLOR=#008000]'Call the mod to get the informatino from the DB
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Call[/COLOR][/SIZE][SIZE=2] getReport([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]
 
Oh, d'uh.... I know what it is now.... and this was something that confused me as well when I was working with this. Aparently even after you create your group(s) in the list view... you then have to add the listviewitms you want in that group to that group (I know, it seems like there should be an easier huh?).

Within some kind of loop or what ever:
VB.NET:
lsvGroup.Items.Add -your listviewItem here-

IT sucks, it really does... but it also gives you the ability to control what does or does not go in the gorup. Warning... any item not put into a specific group will be put in a "DEFAULT" group - that's why you keep getting it.

-tg
 
That worked :) just creating some code now to generate it from the DB and not overwirte the created group.
 
Now I'm having some problem with the header for some reason when I look in the DB and get the New Group, and send the text to the Header it overwrites the old header. I think it's because I'm setting the same object but with a different header value.

Is there a way to create new groups without having to DIM a new listviewgroup?

Because I want to make it possible to have as many group as the DB have, so in case I add a new group to the DB it should add a new group to the listview.

I created this if statement here to check the name, and create a new group, it works, but the problem is that it deletes the old group and add the new value to it.

VB.NET:
[SIZE=2][COLOR=#008000]'Create a Group
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] lvGroup.Header.ToString = myDataRow.Item([/SIZE][SIZE=2][COLOR=#800000]"Name"[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Add lisview item to the group
[/COLOR][/SIZE][SIZE=2]lvItem.Group = lvGroup
[/SIZE][SIZE=2][COLOR=#0000ff]Else
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'Create a new Group
[/COLOR][/SIZE][SIZE=2]lvGroup.Header = myDataRow.Item([/SIZE][SIZE=2][COLOR=#800000]"Name"[/COLOR][/SIZE][SIZE=2])
frm.lsvReport.Groups.Add(lvGroup)
[/SIZE][SIZE=2][COLOR=#008000]'Add lisview item to the group
[/COLOR][/SIZE][SIZE=2]lvItem.Group = lvGroup
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]

any help I it will be great...:)
 
You are in luck. I happen to have my laptop with me that has the code I used this with. Give me a few to fire it up and upload the relevant code.

-tg
 
And here it is....I've added comments to help show what's going on
VB.NET:
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

'These are the object declarations used... besides a counter later, this is all that's needed
        Dim lvItem As ListViewItem
        Dim myGrp As ListViewGroup

'This prevents flickering as items are added and moved on the list view.
        ListView1.BeginUpdate()
'Make sure no col headers are shown
        ListView1.HeaderStyle = ColumnHeaderStyle.None
'And view in LArge Icon mode 
        ListView1.View = View.LargeIcon
'Either of those last two commands can be adjusted to your liking

'Here I create the first list view item
        _User = User.NewUser("Tech", "Gnome", "")
        lvItem = New ListViewItem(_User.ToStringArray, -1)
        lvItem.Tag = _User
        ListView1.Items.Add(lvItem) 'And add it to the list view

'Now create a group
        myGrp = New ListViewGroup("WelcomeUser", "Welcome!")
        ListView1.Groups.Add(myGrp) 'Add the group....
        ListView1.Groups(myGrp.Name).Items.Add(lvItem) 'And add my item to the group

'New item... same object !
        lvItem = New ListViewItem("Current Sales", -1)
        lvItem.Tag = Nothing
        ListView1.Items.Add(lvItem)

'New group... again, recycling the previous group object
        myGrp = New ListViewGroup("TodayStatus", "Where You Are Today...")
        ListView1.Groups.Add(myGrp)
        ListView1.Groups(myGrp.Name).Items.Add(lvItem)

'Here I create and add multiple listviewitems to the group I just created
        myGrp = New ListViewGroup("Today", "Today")
        ListView1.Groups.Add(myGrp)

        lvItem = New ListViewItem("Print Jobs", 0)
        lvItem.Tag = Nothing
        ListView1.Items.Add(lvItem)
        ListView1.Groups(myGrp.Name).Items.Add(lvItem)

        lvItem = New ListViewItem("Appointments", 0)
        lvItem.Tag = Nothing
        ListView1.Items.Add(lvItem)
        ListView1.Groups(myGrp.Name).Items.Add(lvItem)

        lvItem = New ListViewItem("Calls", 0)
        lvItem.Tag = Nothing
        ListView1.Items.Add(lvItem)
        ListView1.Groups(myGrp.Name).Items.Add(lvItem)

        lvItem = New ListViewItem("Tasks", 0)
        lvItem.Tag = Nothing
        ListView1.Items.Add(lvItem)
        ListView1.Groups(myGrp.Name).Items.Add(lvItem)


'At this point, I loop through all of the existing items that are in my list view (created in another button click event) 
'And based on the LastName property of the class stored in the .Tag property...


        For Each lvitm As ListViewItem In ListView1.Items
            If TypeOf lvitm.Tag Is Customer Then

'Create a group...
                myGrp = New ListViewGroup(CType(lvitm.Tag, Customer).LastName, CType(lvitm.Tag, Customer).LastName)

'If that group already exists in the ListView...
                If ListView1.Groups.Contains(myGrp) Then
'Add the lsit view item to the EXISTING group (using the name)
                    ListView1.Groups(myGrp.Name).Items.Add(lvitm)
                Else
'If it does not exist, add the group
                    ListView1.Groups.Add(myGrp)
'And add the item to the group
                    ListView1.Groups(myGrp.Name).Items.Add(lvitm)
                End If
            End If
        Next
'Allow the control up update itself.
        ListView1.EndUpdate()
    End Sub


Hope this helps.

-tg
 
Yeah that helped me a lot.. I had everything right the only problem was when I check for the group name, I didn't use the NEW again, so It was overwriting the last group :)
 
Back
Top