ListView vb.net 2008中的重复项目

时间:2017-07-05 02:28:33

标签: vb.net

我正在尝试删除listview中的重复项。这是我的代码:

Imports System.Data.OleDb
Public Class frmQuizSelector

Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
    Me.Hide()
End Sub

Private Sub lvwQuizzes()
    With Me.lvwListOfQuizzes
        .FullRowSelect = True
        .GridLines = False
        .View = View.Details
        .Columns.Add("QuizID", 1)
        .Columns.Add("Quiz Title", 268)
    End With
End Sub

Private Sub frmQuizSelector_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.cmdPrevQ.Visible = False
    Me.cmdNextQ.Visible = False

    lvwQuizzes()

    Try
        Dim dtb As New DataTable
        Dim strSql As String = "SELECT * FROM tblQuestions"
        AConn.Open()
        Using dataadapter As New OleDbDataAdapter(strSql, AConn)
            dataadapter.Fill(dtb)
        End Using
        AConn.Close()
        Me.lvwListOfQuizzes.Items.Clear()
        Dim i As Integer = 0
        For i = 0 To dtb.Rows.Count - 1
            Dim lv As New ListViewItem
            lv.Text = dtb.Rows(i).Item("QuizID1")
            lv.SubItems.Add(dtb.Rows(i).Item("QuizTitle"))
            lv.SubItems.Add(dtb.Rows(i).Item("CourseSubject"))
            lv.SubItems.Add(dtb.Rows(i).Item("QuestionNumber"))
            lv.SubItems.Add(dtb.Rows(i).Item("ActualQuestion"))
            lv.SubItems.Add(dtb.Rows(i).Item("Answer1"))
            lv.SubItems.Add(dtb.Rows(i).Item("Answer2"))
            lv.SubItems.Add(dtb.Rows(i).Item("Answer3"))
            lv.SubItems.Add(dtb.Rows(i).Item("Answer4"))
            Me.lvwListOfQuizzes.Items.Add(lv)
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
        AConn.Close()
    End Try
End Sub


Private Sub lvwListOfQuizzes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwListOfQuizzes.Click
    Me.cmdNextQ.Visible = True

    Try
        Dim strSql As String = "SELECT * FROM tblQuestions WHERE [QuizID1] =  " & lvwListOfQuizzes.SelectedItems(0).SubItems(0).Text & ""
        Dim dtb As New DataTable
        AConn.Open()
        Using dataadapter As New OleDbDataAdapter(strSql, AConn)
            dataadapter.Fill(dtb)
        End Using
        AConn.Close()
        For Each row As DataRow In dtb.Rows
            Me.lblQuizName.Text = row("QuizTitle").ToString()
            Me.lblCourseSubject.Text = row("CourseSubject").ToString()
            Me.lblQNum.Text = row("QuestionNumber").ToString()
            Me.lblQuestion.Text = row("ActualQuestion").ToString()
            Me.lblA.Text = row("Answer1").ToString()
            Me.lblB.Text = row("Answer2").ToString()
            Me.lblC.Text = row("Answer3").ToString()
            Me.lblD.Text = row("Answer4").ToString()
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
        AConn.Close()
    End Try
End Sub

上面的代码在我的listview中生成重复项。例如,我有五个测验标题在我的数据库中说“国家”(五个标题各有不同的问题),我的列表视图显示了所有五个相同的标题。我只需要它只显示一个,因为它只是一个测验标题

1 个答案:

答案 0 :(得分:0)

你可以这样做,

由于数据在被注入listview之前被分配给数据行,我建议防止重复访问行,而不是在将数据注入listview的任何时候对数据进行长时间的研究。

Dim lastrow As DataRow
Sub tbnewrow(Objet As DataTable, e As DataRowChangeEventArgs)
        If (e.Action = DataRowAction.Commit) Then
            If (Objet.Rows.IndexOf(lastrow) > 0 AndAlso e.Row.Item(1) = Objet.Rows(Objet.Rows.IndexOf(lastrow)).Item(1)) Then
                e.Row.Item(1) = ""
            Else
                lastrow = e.Row
            End If
        End If
End Sub

这可以确保第二列没有重复的行,所有行都在一次加载中重复存放。

现在只需将此事件绑定到您的数据表。

Dim dtb As New DataTable
AddHandler dtb .RowChanged, AddressOf tbnewrow  

在此之后,您可能更喜欢使用datagrid而不是listview,这样可以节省更多时间。