I am trying the below code by not able to execute it properly, because as soon as first if is cleared then the other if statement take the previous i value. that is when i = 2 is accepted then i remain 2 all time.

below is the code i am using

please help me

For i = 0 To lvHistory.Items.Count - 1

        If lvHistory.Items(i).SubItems(0).Text <> 0 Then
            If lastdt = "01-01-1900" Then
                lastdt = lvHistory.Items(i).SubItems(2).Text
            End If

            If lvHistory.Items(i).SubItems(2).Text = lastdt Then

                If lvHistory.Items(i).SubItems(6).Text = "2Diagnosis" Then

                    'Copy all details of Htype(6) = Diagnosis

                    'Inserting values inside Listview
                    Dim j As Integer
                    Dim lst1 As New ListViewItem(j)

                    lst1.SubItems.Add(cmbPatient.SelectedValue)  'PatientId 1
                    lst1.SubItems.Add(dtVisit.Text)  'Visit Date 2

                    lst1.SubItems.Add(lvHistory.Items(i).SubItems(3).Text)  'History Of 3
                    lst1.SubItems.Add(lvHistory.Items(i).SubItems(4).Text)   'HistoryId
                    lst1.SubItems.Add(lvHistory.Items(i).SubItems(5).Text) 'History Details
                    lst1.SubItems.Add(lvHistory.Items(i).SubItems(6).Text) 'History type
                    lst1.SubItems.Add(lvHistory.Items(i).SubItems(7).Text) 'DurationId
                    lst1.SubItems.Add(lvHistory.Items(i).SubItems(8).Text) 'Remarks
                    lst1.SubItems.Add(lvHistory.Items(i).SubItems(9).Text) 'Duration
                    lst1.SubItems.Add(lvHistory.Items(i).SubItems(10).Text) 'DBRemarks

                    lvHistory.Items.Add(lst1)

                    Dim flag As Boolean = True
                    For Each l As ListViewItem In lvHistory.Items

                        Dim strmyGroupname As String = l.SubItems(2).Text

                        For Each lvg As ListViewGroup In lvHistory.Groups

                            If lvg.Name = strmyGroupname Then
                                l.Group = lvg
                                flag = False
                            End If

                        Next

                        If flag = True Then
                            Dim lstGrp As New ListViewGroup(strmyGroupname, strmyGroupname)
                            lvHistory.Groups.Add(lstGrp)
                            l.Group = lstGrp
                        End If

                        flag = True

                    Next

                    ' Figure out the new sorting order.
                    Dim sort_order As System.Windows.Forms.SortOrder = SortOrder.Descending

                    ' Create a comparer.
                    lvHistory.ListViewItemSorter = New ListViewComparer(2, sort_order)

                    ' Sort.
                    lvHistory.Sort()

                End If


            End If

        End If
    Next

You do too many mistake.
The index of subitem of a listviewitem never starts from 0, that you did it in the first line

If lvHistory.Items(i).SubItems(0).Text <> 0 Then .

It starts always from 1. Like

If lvHistory.Items(i).SubItems(1).Text <> 0 Then

I do not understand, why are you declare the variable 'j' and also why have you used it as

Dim j As Integer
Dim lst1 As New ListViewItem(j)

There is no utility of the variable 'j', so discard it. simply use

Dim lst1 As New ListViewItem

And also the ListViewItem Text property is blank. Here a ListViewItem is inserted with a blank Item Text Property but it has some subitems.

Check the variable for the For Loop is 'i' or not

So what, as you like! Your codes are quite right, only check the conditions with appropriate subitem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.