使用vb中的listview搜索功能

时间:2014-05-20 03:45:20

标签: database vb.net

我在搜索数据后在listview的第一列中更改了数据。 例: 在第一列中,数据必须显示类似于" 3234"的房间代码。但在搜索之后,数据变为" Room_Code"。 以下是搜索功能的代码

Private Sub Search()

    ListViewRoom.Items.Clear()

    Dim item As New ListViewItem
    Dim _isFound As Boolean = False

    Dim colName() As String = {"Room_Code", "Room_Type", "Room_No", "Room_Price", "Room_Status", "No_of_Occupancy"}


    Dim strSqlSearch As String = "SELECT Room_Code, Room_Type, Room_No, Room_Price, Room_Status, No_of_Occupancy " & _
                                    "FROM Room " & _
                                    "WHERE " & colName(cboSearch.SelectedIndex) & " LIKE '%" & txtSearch.Text & "%'"

    dbSource = "Data Source=LAILATUL-PC\SERVER;Initial Catalog=HotelManagementSystem;Integrated Security=True"

    Using con As New SqlClient.SqlConnection("Data Source=LAILATUL-PC\SERVER;Initial Catalog=HotelManagementSystem;Integrated Security=True")

        Using com As New SqlClient.SqlCommand()
            With com
                .Connection = con
                .CommandType = CommandType.Text
                .CommandText = strSqlSearch


            End With

            Try
                con.Open()
                Dim dr As SqlClient.SqlDataReader = com.ExecuteReader

                While dr.Read
                    _isFound = True


                    item = ListViewRoom.Items.Add("Room_Code".ToString)
                    item.SubItems.Add(dr("Room_Type".ToString))
                    item.SubItems.Add(dr("Room_No".ToString))
                    item.SubItems.Add(dr("Room_Price".ToString))
                    item.SubItems.Add(dr("Room_Status".ToString))
                    item.SubItems.Add(dr("No_of_Occupancy".ToString))
                End While

                If Not _isFound Then
                    MsgBox("No results found.", MsgBoxStyle.OkOnly, "Information")
                End If
            Catch ex As Exception
                MsgBox(ex.Message.ToString(), MsgBoxStyle.OkOnly, "Error")
            End Try

        End Using
    End Using

This picture is the interface of problem

Tq的

1 个答案:

答案 0 :(得分:0)

嗯,因为你告诉它:

item = ListViewRoom.Items.Add("Room_Code".ToString)

我认为那应该是:

item = ListViewRoom.Items.Add(dr("Room_Code").ToString)

顺便说一下,你还有几个其他无意义的电话给ToString,例如。

item.SubItems.Add(dr("Room_Type".ToString))

在字面上调用ToString的重点是什么?当然,它是您想要转换为String的字段值:

item.SubItems.Add(dr("Room_Type").ToString())

你真的应该使用DataGridView并简单地绑定DataTable而不是滥用ListView,这不是网格控件。

相关问题