在文本框之间搜索值并将其过滤到datagridview

时间:2016-03-13 09:05:06

标签: vb.net datagridview

任何人都可以帮助我吗?我需要一个搜索按钮来查找两个文本框(txtMin和txtMax)之间的值(价格)并将其加载到DataGridview。

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
    con.ConnectionString = "server=localhost; userid=root; password=root; database=realestate"
    Dim SDA As New MySqlDataAdapter
    Dim dbDataSet As New DataTable
    Dim bSource As New BindingSource
    Dim dv As New DataView(dbDataSet)

    Try
        con.Open()
        Dim q1 As String
        q1 = "Select * from realestate.transactions"
        cmd = New MySqlCommand(q1, con)
        SDA.SelectCommand = cmd
        SDA.Fill(dbDataSet)
        bSource.DataSource = dbDataSet
        frmHome.DataGridView1.DataSource = bSource
        SDA.Update(dbDataSet)
        dv.RowFilter = String.Format("[propertyid] like '%{0}%' AND [type] like '%{1}%' **AND Convert(price,'System.String') Like '%{2}%'** AND Convert(nobed,'System.String') Like '%{3}%' AND Convert(nobath,'System.String') Like '%{4}%' AND Convert(footages,'System.String') Like '%{5}%' AND [location] like '%{6}%' AND [status] like '%{7}%'", txtProperty.Text, cmbType.Text, **txtMin.Text**, cmbBed.Text, cmbBath.Text, txtFootages.Text, txtLocation.Text, cmbStatus.Text)
        frmHome.DataGridView1.DataSource = dv
        If frmHome.DataGridView1.RowCount > 0 Then
            MessageBox.Show("Search Successful")
        Else
            MessageBox.Show("No records found")
        End If
        con.Close()
        txtProperty.Text = ""
        cmbType.Text = ""
        txtFootages.Text = ""
        txtMax.Text = ""
        txtMin.Text = ""
        cmbBath.Text = ""
        cmbBed.Text = ""
        cmbStatus.Text = ""
        Me.Hide()
        frmHome.DataGridView1.Columns(0).SortMode = DataGridViewColumnSortMode.NotSortable
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

在我的代码中,它只会搜索我在txtMin.text中输入的值(价格)。我将如何对此进行编码以搜索txtMin.text和txtMax.text之间的值。请参阅附图以获取参考资料

Search Image

2 个答案:

答案 0 :(得分:0)

您的数据库中的价格应该有一个十进制数字字段值。 然后,您可以更新过滤器,以搜索您在txtMin和txtMax中输入的值之间的值。

假设您的过滤器已经过编译,您可以按照以下步骤进行更新:

dv.RowFilter = 
String.Format("
[propertyid] like '%{0}%' AND 
[type] like '%{1}%' **AND 
price >{2} AND price<{3} AND 
Convert(nobed,'System.String') Like '%{4}%' AND 
Convert(nobath,'System.String') Like '%{5}%' AND 
Convert(footages,'System.String') Like '%{6}%' AND 
[location] like '%{7}%' AND 
[status] like '%{8}%'", 
txtProperty.Text, cmbType.Text, Single.Parse(txtMin.Text),     Single.Parse(txtMax.Text), cmbBed.Text, cmbBath.Text, txtFootages.Text,     txtLocation.Text, cmbStatus.Text)

对于素材,采用相同的方法: 您需要为片段范围添加两个文本框:txtFootageMin和txtFootageMax:

dv.RowFilter = 
String.Format("
[propertyid] like '%{0}%' AND 
[type] like '%{1}%' **AND 
price >{2} AND price<{3} AND 
Convert(nobed,'System.String') Like '%{4}%' AND 
Convert(nobath,'System.String') Like '%{5}%' AND 
footages >{6} AND footages<{7} AND 
[location] like '%{8}%' AND 
[status] like '%{9}%'", 
txtProperty.Text, cmbType.Text, Single.Parse(txtMin.Text),     Single.Parse(txtMax.Text), cmbBed.Text, cmbBath.Text,    Single.Parse(txtFootageMin.Text), Single.Parse(txtFootageMax.Text),       txtLocation.Text, cmbStatus.Text)

答案 1 :(得分:0)

以下是素材的代码以及价格:

Dim result As Single
    If String.IsNullOrEmpty(txtMin.Text.Trim()) Then
        result = Single.MinValue
    Else
        result = Single.Parse(txtMin.Text)
    End If
    Dim result2 As Single
    If String.IsNullOrEmpty(txtMax.Text.Trim()) Then
        result2 = Single.MaxValue
    Else
        result2 = Single.Parse(txtMax.Text)
    End If
    Dim result3 As Single
    If String.IsNullOrEmpty(txtFootMin.Text.Trim()) Then
        result3 = Single.MaxValue
    Else
        result3 = Single.Parse(txtFootMin.Text)
    End If
    Dim result4 As Single
    If String.IsNullOrEmpty(txtFootMax.Text.Trim()) Then
        result4 = Single.MaxValue
    Else
        result4 = Single.Parse(txtFootMax.Text)
    End If
dv.RowFilter = String.Format("[propertyid] like '%{0}%' AND [type] like '%{1}%' AND price >={2} AND price<={3} AND Convert(nobed,'System.String') Like '%{4}%' AND Convert(nobath,'System.String') Like '%{5}%' AND footages >={6} AND footages<={7} AND [location] like '%{8}%' AND [status] like '%{9}%'", txtProperty.Text, cmbType.Text, result, result2, cmbBed.Text, cmbBath.Text, result3, result4, txtLocation.Text, cmbStatus.Text)