Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the following sub to populate my list. i want to insert another column at run time to automatically number the records in the list view.therefore the numbering should start from 1 and 2,3,4,5 and so on. in other words i want to insert an auto increment column at run time and this column must be placed in the leftmost position.


VB
Public Sub populate_lvw()
        lvw.Items.Clear()
        con.Close()

        con.Open()
        cmd = New SqlCommand("SELECT * FROM Students s  join (select  student_fullname, student_dob   from Students GROUP BY student_fullname, student_dob HAVING count(*) > 1) a on s.student_fullname = a.student_fullname and s.student_dob=a.student_dob order by s.student_fullname", con)
        Using dr As SqlDataReader = cmd.ExecuteReader

            If dr.HasRows Then
                While dr.Read
                    Dim li As New ListViewItem()
                    li.Text = Convert.ToString(dr.Item("student_slno"))
                    li.SubItems.Add(Convert.ToString(dr.Item("student_fullname")))
                    li.SubItems.Add(CType(dr.Item("student_dob"), DateTime).ToShortDateString)
                    'li.SubItems.Add(Convert.ToString(dr.Item("student_dob")))
                    li.SubItems.Add(Convert.ToString(dr.Item("student_branch")))
                    li.SubItems.Add(Convert.ToString(dr.Item("student_scholarship")))
                    li.SubItems.Add(Convert.ToString(dr.Item("student_accountno")))

                    '  li.SubItems.Add(dr.Item("student_dob")).ToString("dd/MM/yyyy")
                    lvw.Items.Add(li)
                End While

            Else : MsgBox(" No defaulters found", MsgBoxStyle.OkOnly)

            End If

        End Using
        cmd.Dispose()
        con.Close()


    End Sub
Posted
Updated 16-Apr-15 23:56pm
v2

1 solution

VB
While myListData.Read       ' using ExecuteReader

Dim i As Integer

       For i = 0 To lvList.Items.Count - 1
          lvList.Items(i).Text = (i + 1).ToString()
       Next i

lvList.Items.Add(Trim(CStr(CInt(i) + 1).ToString)) 'col no. 1
   ' adding sub items from database (mysql)
   ' ........
End While


Hope it will help you
 
Share this answer
 
Comments
hlsc1983 17-Apr-15 10:20am    
But in my case I can know the number of items only after all the rows have been added. So I don't understand how I can use your solution.thanks
Member 12875686 20-Apr-19 4:46am    
adding +1 in empty row also at end

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900