narrow search (URGENT)

  • 15 years ago

    I need to make a form for searching data from one table. Table is in dataset.


    Problem is that on form I need few textboxes witch user will use to make a query. therefor in table are several columns that are used for retreaveing values.


    User will when he enters text in textbox press enter. In datagrid then neads to be displayed rows that fit his search.  that isn't problem for one textbox (one search value).


    Table ex. :


    ID_number, Name, Lastname, Country, city...


    How do I narrow my search ...other textboxes inputs


    Textbox1 - Name
    Textbox2 - Lastname
    Textbox3 - Country
    Textbox4 - City


    For searching one colum I used dataview property "rowfilter" that works..

  • 15 years ago

    You simply need to set your RowFilter based on the values in the four TextBoxes:

    Code:
    myDataTable.DefaultView.RowFilter = String.Format("Name = '{0}' AND LastName = '{1}' AND Country = '{2}' AND City = '{3}'", TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text)
    If you want to ignore empty TextBoxes then you would need to use a series of If statements to build the RowFilter:Code:
    Dim rowFilter As String = String.Empty


    If TextBox1.Text.Trim().Length > 0 Then
       rowFilter &= "Name = '" & TextBox1.Text.Trim() & "'"
    End If


    If TextBox2.Text.Trim().Length > 0 Then
       If rowFilter.Length > 0 Then
           rowFilter &= " AND "
       End If


       rowFilter &= "LastName = '" & TextBox2.Text.Trim() & "'"
    End If


    If TextBox3.Text.Trim().Length > 0 Then
       If rowFilter.Length > 0 Then
           rowFilter &= " AND "
       End If


       rowFilter &= "Country = '" & TextBox3.Text.Trim() & "'"
    End If


    If TextBox4.Text.Trim().Length > 0 Then
       If rowFilter.Length > 0 Then
           rowFilter &= " AND "
       End If


       rowFilter &= "City = '" & TextBox4.Text.Trim() & "'"
    End If


    myTable.DefaultView.RowFilter = rowFilter

    Finally, can I suggest using more descriptive names for your TextBoxes.

  • 15 years ago
    Jmci I dont know how to thank you

    Simply... you saved me!

    This is exactly what I needed.

    for nameing textboxes I use descriptive names.

    Thanx again!

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“My definition of an expert in any field is a person who knows enough about what's really going on to be scared.” - P. J. Plauger