Results 1 to 11 of 11

Thread: [vb.net expr]send text onchange form <---> class --> update dgv

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    103

    Question [vb.net expr]send text onchange form <---> class --> update dgv

    I have a question and hope it's possible

    i have a form with a datagridview with textboxes above (see picture).
    What i want to do is from the moment i type a character in a textbox he start searching in the datagridview. With other words... he has to look for it on textChanged.

    i taught i make in my form a function

    VB Code:
    1. Function selectCustomer() As String
    2.         Dim i, t As Integer
    3.         Dim searchstring As String
    4.         Dim string1, string2, string3 As String
    5.         Dim name, lastname, address, zipcode, city, speciality, phone As String
    6.  
    7.         name = Me.txtSearchCustomerName.Text
    8.         lastname = Me.txtSearchCustomerLastname.Text
    9.         address = Me.txtSearchCustomerAddress.Text
    10.         zipcode = Me.txtSearchCustomerZipcode.Text
    11.         city = Me.txtSearchCustomerCity.Text
    12.         speciality = Me.cmbSearchCustomerSpeciality.Text
    13.         phone = Me.txtSearchCustomerTelephone.Text
    14.  
    15.  
    16.         string1 = "SELECT ... FROM ... "
    17.         string3 = ";"
    18.         string2 = String.Concat("WHERE ", strCustomerName, " LIKE '%", name, "%' AND '", strCustomerLastName, "LIKE '%", lastname, "%'etc...")
    19.  
    20.         searchstring = String.Concat(string1, string2, string3)
    21.         Return searchstring
    22.     End Function

    from the moment the text in a textbox change, it has to send (don't know how) the string (searchstring) to the class data_function where it makes a connection with the databank, get the data from the databank and send it back to the form and update the datagridview. And that for all the characters i type in all the textboxes (+1 combobox) i have above the datagridview.

    is this possible and how...
    (if it is not a problem for you, please explain with some code...)

    i hope its clear for you

    greetz freakyme
    Attached Images Attached Images  
    Last edited by freakyme; Mar 12th, 2007 at 04:37 AM.
    VB.NET 2005 n00b

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [vb.net expr]send text onchange form <---> class --> update dgv

    You do NOT want to be querying the database every time the user changes a character. What if changing a few characters doesn't change the result? You've just queries the database several times for exactly the same data.

    You should perform one query to get ALL the data into a single DataTable or collection of business objects. You then bind that table or collection to a BindingSource and bind that to your grid. You now write a single method to handle the TextChanged event of all TextBoxes as well as the SelectedIndexChanged event of the ComboBox. In that event handler you call your 'selectCustomer' method and assign the result to the Filter property of the BindingSource. Voila!

    Also, "selectCustomer" is a bad name for that method. The name of a method is supposed to correspond to what it does and that method does NOT select a customer. What is does do is get a string to filter your customer records, so it should be named something like "GetCustomerFilter".

    Finally, String.Concat is the wrong method to use in that case. You should be using String.Format. For example, to create a string suitable to assign to a BindingSource.Filter property you'd do something like this:
    vb Code:
    1. myBindingSource.Filter = String.Format("FirstName LIKE '{0}%' AND LastName LIKE '{1}%'", Me.firstNameText.Text, Me.lastNameText.Text)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    103

    Re: [vb.net expr]send text onchange form <---> class --> update dgv

    hmmm... that don't work


    form
    VB Code:
    1. Dim filldatagrid As New Data_functions
    2.         Dim dt As DataTable
    3.         Dim bs As New BindingSource
    4.         Dim rowcount As Integer
    5.         dt = filldatagrid.fillCustomerSearchField
    6.         bs.DataSource = dt
    7.         bs.Filter = String.Format("Lastname LIKE '{0}%' AND Firstname LIKE '{1}%'", Me.txtSearchCustomerName.Text, Me.txtSearchCustomerLastname.Text)
    8.         Me.dgrSearchCustomer.DataSource = bs


    class:
    VB Code:
    1. Function fillCustomerSearchField() As DataTable
    2.         Dim connectionstring As New Connection
    3.         Dim dt As DataTable
    4.         Dim connstr As String = connectionstring.getConnectionString
    5.         Dim sqlstr As String
    6.         Dim command As New SqlClient.SqlCommand
    7.         Dim connection As New Data.SqlClient.SqlConnection(connstr)
    8.         sqlstr = String.Concat("SELECT c.pkCustomerId 'PkCust.',c.strCustomerName 'Lastname', c.strCustomerFirstname 'Firstname', replace(a.strAddressFull, '_', ' ') 'Address', ci.strCityPostalCode 'Zipc.', ci.strCityName 'City', cg.strCustomerGenderName 'Speciality', c.strCustomerGsmNumber 'GSM Number' FROM tblAddress a, tblCity ci, tblCustomer c, tblCustomerAddress ca, tblCustomerGender cg WHERE c.pkCustomerId = ca.fkCustomerId AND ca.fkAddressId = a.pkAddressId AND a.fkCityId = ci.pkCityId AND cg.pkCustomerGenderId = c.fkCustomerGenderId ORDER BY 2")
    9.         command.CommandText = sqlstr
    10.         Try
    11.             connection.Open()
    12.             command.CommandType = CommandType.Text
    13.             command.Connection = connection
    14.             Dim da As New SqlClient.SqlDataAdapter(command)
    15.             dt = New DataTable
    16.             da.Fill(dt)
    17.  
    18.         Catch ex As Exception
    19.             MsgBox(Err.Description)
    20.             Throw
    21.         Finally
    22.             connection.Close()
    23.         End Try
    24.         Return dt
    25.     End Function
    Last edited by freakyme; Mar 9th, 2007 at 06:17 AM.
    VB.NET 2005 n00b

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    103

    Unhappy Re: [vb.net expr]send text onchange form <---> class --> update dgv

    ... what do i wrong, i read the help about bindginsource en msdn, but still not found a proper sollution...
    VB.NET 2005 n00b

  5. #5
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [vb.net expr]send text onchange form <---> class --> update dgv

    Hi,

    I think you can use the auto-complete textbox for that.
    Here's a link with an example:

    http://www.codeproject.com/cs/miscct...eteControl.asp

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    103

    Re: [vb.net expr]send text onchange form <---> class --> update dgv

    no that's not the problem... he still don't want to filter...
    and i don't really want a autocomplete txtbx, i want the datagridview directly filtering his data, by typing in the textbox's above
    VB.NET 2005 n00b

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    103

    Unhappy Re: [vb.net expr]send text onchange form <---> class --> update dgv

    is there someone knows where the probleme in the code is why he don't filter the data???
    VB.NET 2005 n00b

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [vb.net expr]send text onchange form <---> class --> update dgv

    It will work as I said if you follow the advice I gave. Start a new project, add two Labels, two TextBoxes, a DataGridView and a BindingSource so that the form looks like the screen shot below. Name the TextBoxes "firstNameText" and "lastNameText". Now add this code:
    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     Dim dt As New DataTable
    3.  
    4.     dt.Columns.Add("ID", GetType(Integer))
    5.     dt.Columns.Add("FirstName", GetType(String))
    6.     dt.Columns.Add("LastName", GetType(String))
    7.  
    8.     'Populate the table with all the data once and once only.
    9.     dt.Rows.Add(1, "John", "Smith")
    10.     dt.Rows.Add(2, "Peter", "Johnson")
    11.     dt.Rows.Add(3, "Emma", "James")
    12.     dt.Rows.Add(4, "Paul", "Gutheridge")
    13.     dt.Rows.Add(5, "Patty", "Jacobson")
    14.     dt.Rows.Add(6, "Andrew", "Peterson")
    15.     dt.Rows.Add(7, "Linda", "Smith")
    16.     dt.Rows.Add(8, "John", "Williams")
    17.     dt.Rows.Add(9, "Jenny", "Andrews")
    18.     dt.Rows.Add(10, "Karen", "Williamson")
    19.  
    20.     Me.BindingSource1.DataSource = dt
    21.     Me.DataGridView1.DataSource = Me.BindingSource1
    22. End Sub
    23.  
    24. Private Sub lastNameText_TextChanged(ByVal sender As System.Object, _
    25.                                      ByVal e As System.EventArgs) Handles lastNameText.TextChanged, _
    26.                                                                           firstNameText.TextChanged
    27.     'Set a new filter each time the text changes in any search field.
    28.     Me.BindingSource1.Filter = String.Format("FirstName LIKE '{0}%' AND LastName LIKE '{1}%'", _
    29.                                              Me.firstNameText.Text, _
    30.                                              Me.lastNameText.Text)
    31. End Sub
    Now run the project and type into the TextBoxes and you'll see that the data in the grid gets filtered accordingly.
    Attached Images Attached Images  
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    103

    Re: [vb.net expr]send text onchange form <---> class --> update dgv

    wooooooooooops, me stupid
    somthimes i have to think more logical...
    sorry...

    thanks...
    greetz freakyme
    VB.NET 2005 n00b

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    103

    Re: [vb.net expr]send text onchange form <---> class --> update dgv

    VB Code:
    1. bs.Filter = String.Format("Lastname LIKE '%{0}%' AND Firstname LIKE '%{1}%' AND Address LIKE '%{2}%' AND Zip LIKE '%{3}%' AND City LIKE '%{4}%' AND Speciality LIKE '{5}%' AND GSM_Number LIKE '%{6}%'", Me.txtSearchCustomerName.Text, Me.txtSearchCustomerLastname.Text, Me.txtSearchCustomerAddress.Text, Me.txtSearchCustomerZipcode.Text, Me.txtSearchCustomerCity.Text, Me.cmbSearchCustomerSpeciality.Text, Me.txtSearchCustomerTelephone.Text)

    in this code, GSM_Number is somtimes NULL...
    when i make a selection on all the other textboxes (or combobox) he don't show the result if there is a null in the gsm_number column...
    how do i have to resolve this in vb.net?

    greetz freakyme
    Last edited by freakyme; Mar 12th, 2007 at 10:17 AM.
    VB.NET 2005 n00b

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    103

    Re: [vb.net expr]send text onchange form <---> class --> update dgv

    isn't that possible???
    hmmm... i thought i use nvl() but that just don't work :s
    VB.NET 2005 n00b

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width