我正在从Access数据库填充ListView数据。我想要做的是添加自动生成的行号。我出于此目的使用以下代码
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
当我使用此代码时,数字会替换数据库中的第一列数据。 我需要的是在第一列显示行号,然后在第2列和第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
答案 0 :(得分:2)
我需要显示第一列中的行号,然后在第2列和第3列显示数据库中的数据。
因此,事实证明,问题与行号没有任何关系,只是因为你在错误的列中显示实际数据。您已经知道如何在特定列中显示数据,因为您已经在执行此操作:
ListView1.Items.Add(myrow.Item(1))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(myrow.Item(2))
该代码将数据放入第一列和第二列。如果您真正想要的是第一列中有一个数字,然后是第二列和第三列中的数据,那么显而易见的事情就是在第一列中放置一个数字,在第二列和第三列中放入数据。您知道如何将文本放在第一列中,因为您已经在执行此操作,因此请将该文本作为数字。您知道如何将文本放在第二列中,因为您已经在执行此操作,因此请将该文本设置为DataRow
中的相应字段。如果您知道如何将文本添加到第二列,那么您就知道如何将文本添加到第三列和每个后续列。
替代语法:
With ListView1.Items.Add(ListView1.Items.Count + 1)
.SubItems.Add(myRow.Item(2))
End With
答案 1 :(得分:-1)
问题解决了:)
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
答案 2 :(得分:-1)
Sub lstvpop()
ListView1.Items.Clear()
Dim item As ListViewItem
Dim reader As SqlDataReader = db.fetchRows("Select * from tbl_class")
While (reader.Read())
item = ListView1.Items.Add(ListView1.Items.Count + 1)
item.Tag = reader(0)
item.SubItems.Add(reader(1).ToString())
End While
End Sub