在视图列表中选择数据

时间:2010-11-29 04:28:10

标签: vb.net ms-access

我想问一个忙。我使用viewlist显示访问vb.net的数据,但是如果我想选择一个名称则不能。你能帮助我吗?编码已经完成,但我不知道故障在哪里

Imports System.Data.OleDb
Imports System.Data
Public Class Guest_List
    Dim cnn As OleDbConnection
    Dim cmmd As OleDbCommand
    Dim dReader As OleDbDataReader
    Public title, FirstName, LastName, Address, Country, Company, DateIn, DateOut, RoomType, Note As String
    Private Sub Guest_List_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call list_data()
    End Sub

    Private Sub txtKataKunci_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtKataKunci.TextChanged
        Call list_data()
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        Call pilih()
    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Call pilih()
    End Sub

    Private Sub list_data()

        Call clear_list()
        Dim sqlx As String
        Dim x As Integer

        sqlx = "select title,firstname,lastname,address,country,company,datein,dateout,roomtype,notes from tcekin where firstname like '%" & Trim(txtKataKunci.Text) & "%' order by firstname  asc"

        cnn = New OleDbConnection(strConn)
        If cnn.State <> ConnectionState.Closed Then cnn.Close()
        cnn.Open()
        cmmd = New OleDbCommand(sqlx, cnn)
        dReader = cmmd.ExecuteReader

        Try
            While dReader.Read = True
                x = Val(counter.Text)
                counter.Text = Str(Val(counter.Text) + 1)

                With ListView1
                    .Items.Add("")
                    .Items(ListView1.Items.Count - 1).SubItems.Add("")
                    .Items(ListView1.Items.Count - 1).SubItems.Add("")
                    .Items(ListView1.Items.Count - 1).SubItems.Add("")
                    .Items(ListView1.Items.Count - 1).SubItems.Add("")
                    .Items(ListView1.Items.Count - 1).SubItems.Add("")
                    .Items(ListView1.Items.Count - 1).SubItems.Add("")
                    .Items(ListView1.Items.Count - 1).SubItems.Add("")
                    .Items(ListView1.Items.Count - 1).SubItems.Add("")
                    .Items(ListView1.Items.Count - 1).SubItems.Add("")
                    .Items(ListView1.Items.Count - 1).SubItems.Add("")
                    .Items(x).SubItems(0).Text = dReader.GetString(0)
                    .Items(x).SubItems(1).Text = dReader.GetString(1)
                    .Items(x).SubItems(2).Text = dReader.GetString(2)
                    .Items(x).SubItems(3).Text = dReader.GetString(3)
                    .Items(x).SubItems(4).Text = dReader.GetString(4)
                    .Items(x).SubItems(5).Text = dReader.GetString(5)
                    .Items(x).SubItems(6).Text = Format(CDate(dReader.GetDateTime(6)), "dd-MMMM-yyyy")
                    .Items(x).SubItems(7).Text = Format(CDate(dReader.GetDateTime(7)), "dd-MMMM-yyyy")
                    .Items(x).SubItems(8).Text = dReader.GetString(8)
                    .Items(x).SubItems(9).Text = dReader.GetString(9)
                End With
            End While
        Finally
            dReader.Close()
        End Try
        cnn.Close()
    End Sub

    Private Sub clear_list()
        While Val(counter.Text) > 0
            ListView1.Items(0).Remove()
            counter.Text = Val(counter.Text) - 1
        End While
    End Sub

    Private Sub pilih()
        Try
            title = ListView1.SelectedItems(0).SubItems(0).Text.ToString
            FirstName = ListView1.SelectedItems(0).SubItems(1).Text.ToString
            LastName = ListView1.SelectedItems(0).SubItems(2).Text.ToString
            Address = ListView1.SelectedItems(0).SubItems(3).Text.ToString
            Country = ListView1.SelectedItems(0).SubItems(4).Text.ToString
            Company = ListView1.SelectedItems(0).SubItems(5).Text.ToString
            DateIn = ListView1.SelectedItems(0).SubItems(6).Text.ToString
            DateOut = ListView1.SelectedItems(0).SubItems(7).Text.ToString
            RoomType = ListView1.SelectedItems(0).SubItems(8).Text.ToString
            Note = ListView1.SelectedItems(0).SubItems(9).Text.ToString
            Me.Close()
        Catch ex As Exception
            MsgBox("pilih salah satu data", MsgBoxStyle.Information)
        End Try

    End Sub
End Class

2 个答案:

答案 0 :(得分:1)

“选择一个名字”是什么意思?您是否尝试从数据库中查询特定名称?

将sqlx更改为

String.Format("select title,firstname,lastname,address,country,company,datein,dateout,roomtype,notes from tcekin where firstname like '%{0}%' order by firstname  asc", txtKataKunci.Text.Trim())

然后在阅读器上添加BreakPoint,点击F5并查看是否有任何数据从数据库返回。您还可以尝试直接在访问数据库上运行查询。

答案 1 :(得分:0)

尝试使用BindingSource

将datareader绑定到列表视图

这样您就可以获得sql语句返回的所有记录。