Rabeea Qabaha Rabeea Qabaha - 3 months ago 62
Vb.net Question

Auto Generate Row Number in listview

I'm filling ListView data from access database. What I'm trying to do is to add auto generated row number. I use the following code for the purpose

Private Sub row_num()
Dim i As Integer
For i = 0 To ListView1.Items.Count - 1
ListView1.Items(i).Text = i + 1.ToString
Next
End Sub


the problem when i use this code the numbers replace the first column of data from database.
what i need show the row number in the first column , then show the data from the database in column 2 and 3 .

like this :
num name phone
1 Rabeea 0521234567
2 mark 0527654321


Public Sub showlistview()
Dim cyear As String
If ComboBox1.Text = "2014" Then
cyear = "pay_2014"
ElseIf ComboBox1.Text = "2015" Then
cyear = "pay_2015"
ElseIf ComboBox1.Text = "2016" Then
cyear = "pay_2016"
ElseIf ComboBox1.Text = "2017" Then
cyear = "pay_2017"
ElseIf ComboBox1.Text = "2018" Then
cyear = "pay_2018"
ElseIf ComboBox1.Text = "2019" Then
cyear = "pay_2019"
End If

Dim cmonth As String

If ComboBox2.Text = "January_1" Then
cmonth = "jan_1"
ElseIf ComboBox2.Text = "February_2" Then
cmonth = "feb_2"
ElseIf ComboBox2.Text = "March_3" Then
cmonth = "mar_3"
ElseIf ComboBox2.Text = "April_4" Then
cmonth = "apr_4"
ElseIf ComboBox2.Text = "May_5" Then
cmonth = "may_5"
ElseIf ComboBox2.Text = "June_6" Then
cmonth = "jun_6"
ElseIf ComboBox2.Text = "July_7" Then
cmonth = "jul_7"
ElseIf ComboBox2.Text = "August_8" Then
cmonth = "aug_8"
ElseIf ComboBox2.Text = "September_9" Then
cmonth = "sep_9"
ElseIf ComboBox2.Text = "October_10" Then
cmonth = "oct_10"
ElseIf ComboBox2.Text = "November_11" Then
cmonth = "nov_11"
ElseIf ComboBox2.Text = "December_12" Then
cmonth = "dec_12"
End If

Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter("select * from " & cyear & " where " & cmonth & "=0", con)
da.Fill(dt)
Dim myrow As DataRow
For Each myrow In dt.Rows
ListView1.Items.Add(myrow.Item(1))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(2))

Next
End Sub

Answer

problem solved :)

Public Sub showlistview()
    Dim cyear As String
    If ComboBox1.Text = "2014" Then
        cyear = "pay_2014"
    ElseIf ComboBox1.Text = "2015" Then
        cyear = "pay_2015"
    ElseIf ComboBox1.Text = "2016" Then
        cyear = "pay_2016"
    ElseIf ComboBox1.Text = "2017" Then
        cyear = "pay_2017"
    ElseIf ComboBox1.Text = "2018" Then
        cyear = "pay_2018"
    ElseIf ComboBox1.Text = "2019" Then
        cyear = "pay_2019"
    End If

    Dim cmonth As String

    If ComboBox2.Text = "January_1" Then
        cmonth = "jan_1"
    ElseIf ComboBox2.Text = "February_2" Then
        cmonth = "feb_2"
    ElseIf ComboBox2.Text = "March_3" Then
        cmonth = "mar_3"
    ElseIf ComboBox2.Text = "April_4" Then
        cmonth = "apr_4"
    ElseIf ComboBox2.Text = "May_5" Then
        cmonth = "may_5"
    ElseIf ComboBox2.Text = "June_6" Then
        cmonth = "jun_6"
    ElseIf ComboBox2.Text = "July_7" Then
        cmonth = "jul_7"
    ElseIf ComboBox2.Text = "August_8" Then
        cmonth = "aug_8"
    ElseIf ComboBox2.Text = "September_9" Then
        cmonth = "sep_9"
    ElseIf ComboBox2.Text = "October_10" Then
        cmonth = "oct_10"
    ElseIf ComboBox2.Text = "November_11" Then
        cmonth = "nov_11"
    ElseIf ComboBox2.Text = "December_12" Then
        cmonth = "dec_12"
    End If

    Dim dt As New DataTable
    Dim ds As New DataSet
    ds.Tables.Add(dt)
    Dim da As New OleDbDataAdapter("select * from " & cyear & " where " & cmonth & "=0", con)
    da.Fill(dt)
    Dim myrow As DataRow
    For Each myrow In dt.Rows
        'ListView1.Items.Add(myrow.Item(1))
        'ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(2))
        Dim i As Integer
        Dim listViewItem As New ListViewItem
        listViewItem.SubItems.Add("0")
        listViewItem.SubItems.Add("1")
        listViewItem.SubItems.Add("2")
        For i = 0 To ListView1.Items.Count
            listViewItem.SubItems(0).Text = i + 1.ToString
        Next
        listViewItem.SubItems(1).Text = myrow.Item(1)
        listViewItem.SubItems(2).Text = myrow.Item(2)
        ListView1.Items.Add(listViewItem)
    Next
Comments