Question Databounded textboxes stop showing related data

gate7cy

Well-known member
Joined
May 11, 2009
Messages
119
Programming Experience
3-5
Hello to all and thank in advance for the time sacrificed on your behalf for my little problem. My question is a little unique and is too long to google it ( even though I have tried it) so I thought to try here.

I have a form with a datagrid and some textboxes. Both , the datagrid and textboxes are bounded to the same table and when I move along the datagrid my textboxes show the related text. In the same form I have a search textbox to do some searching/filtering of the datagrid view. My search works fine but when I try to click or navigate the datagrid after a search the textboxes do not respond and dont show the related data. Instead they remain showing the last data before the search was performed. How to make the textboxes show what I want after a search.
Thanks for all the help . Thank you......:eek:
 
Here it is. Have a look.

VB.NET:
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class modcustomer
    Public frmSale As Sale
    Public frmnewservice As IntRMA

    Private Sub CustomerBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.CustomerBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.Database2DataSet)

    End Sub

    Private Sub customer2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Database2DataSet.Customer' table. You can move, or remove it, as needed.
        Me.CustomerTableAdapter.Fill(Me.Database2DataSet.Customer)
        'TODO: This line of code loads data into the 'Database2DataSet.Customer' table. You can move, or remove it, as needed.
        Me.CustomerTableAdapter.Fill(Me.Database2DataSet.Customer)

    End Sub

    Private Sub CustomerBindingNavigatorSaveItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Validate()
        Me.CustomerBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.Database2DataSet)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Me.Close()
    End Sub

    Public Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


        If Not IsNothing(frmSale) Then
            frmSale.IdTextBox.Text = Me.CustomerIDTextBox.Text
            frmSale.NameTextBox.Text = Me.CustNameTextBox.Text
            frmSale.SurnameTextBox.Text = Me.SurnameTextBox.Text
            frmSale.NickTextBox.Text = Me.NickTextBox.Text
            frmSale.TelTextBox.Text = Me.TelephoneTextBox.Text
            frmSale.MobTextBox.Text = Me.MobileTextBox.Text
            frmSale.Tel2TextBox.Text = Me.OtherTelTextBox.Text
            frmSale.FaxTextBox.Text = Me.FaxTextBox.Text
            frmSale.TownTextBox.Text = Me.TownTextBox.Text
            frmSale.StreetTextBox.Text = Me.StreetTextBox.Text
            frmSale.BlockTextBox.Text = Me.BlockTextBox.Text
            frmSale.HouseNumTextBox.Text = Me.HouseNumTextBox.Text
            frmSale.FlatTextBox.Text = Me.FlatNumTextBox.Text
            frmSale.PostcodeTextBox.Text = Me.PostCodeTextBox.Text
            frmSale.EmailTextBox.Text = Me.OtherTextBox.Text
            Sale.Show()
            Me.Close()

        End If

    End Sub

    Private Sub CustomerBindingNavigator_Refre****ems(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomerBindingNavigator.Refre****ems

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        IntRMA.telTextBox.Text = Me.TelephoneTextBox.Text
        IntRMA.nameTextBox.Text = Me.CustNameTextBox.Text + " " + Me.SurnameTextBox.Text
        IntRMA.CustidTextBox.Text = Me.CustomerIDTextBox.Text
        IntRMA.telTextBox.Text = Me.TelephoneTextBox.Text

        Me.Close()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()

        newcustomer.Show()

    End Sub


    Private Sub ToolStripTextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripTextBox1.TextChanged
        Dim Search1 As String = Me.ToolStripTextBox1.Text

        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\KoRnHoLiOs\Documents\Database2.accdb;Persist Security Info=False;")
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Customer WHERE CustName Like '%" & Search1 & "%' OR Surname Like '%" & Search1 & "%' OR Nick Like '%" & Search1 & "%' OR Telephone Like '%" & Search1 & "%' ", con)
        con.Open()
        Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        Dim myDataSet As Database2DataSet = New Database2DataSet()
        myDA.Fill(myDataSet, "Customer")
        CustomerDataGridView.DataSource = myDataSet.Tables("Customer").DefaultView



    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        newcustomer.NameTextBox.Text = Me.CustNameTextBox.Text
        newcustomer.SurnameTextBox.Text = Me.SurnameTextBox.Text
        newcustomer.NickTextBox.Text = Me.NickTextBox.Text
        newcustomer.TelTextBox.Text = Me.TelephoneTextBox.Text
        newcustomer.MobTextBox.Text = Me.MobileTextBox.Text
        newcustomer.Tel2TextBox.Text = Me.OtherTelTextBox.Text
        newcustomer.FaxTextBox.Text = Me.FaxTextBox.Text
        newcustomer.TownTextBox.Text = Me.TownTextBox.Text
        newcustomer.StreetTextBox.Text = Me.StreetTextBox.Text
        newcustomer.NumTextBox.Text = Me.HouseNumTextBox.Text
        newcustomer.BlockTextBox.Text = Me.BlockTextBox.Text
        newcustomer.FlatTextBox.Text = Me.FlatNumTextBox.Text
        newcustomer.PostcodeTextBox.Text = Me.PostCodeTextBox.Text
        newcustomer.EmailTextBox.Text = Me.EmailTextBox.Text
        newcustomer.OtherTextBox.Text = Me.OtherTextBox.Text
        newcustomer.IDTextBox.Text = Me.CustomerIDTextBox.Text



        newcustomer.Show()

        
    End Sub


    Private Sub CustomerDataGridView_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles CustomerDataGridView.CellDoubleClick
       
        IntRMA.nameTextBox.Text = Me.CustNameTextBox.Text + " " + Me.SurnameTextBox.Text
        IntRMA.telTextBox.Text = Me.TelephoneTextBox.Text
        IntRMA.CustidTextBox.Text = Me.CustomerIDTextBox.Text

        IntRMA.Show()
        Me.Close()


    End Sub

    Private Sub CustomerDataGridView_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles CustomerDataGridView.CellValueChanged
        Me.Validate()
        Me.CustomerBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.Database2DataSet)

    End Sub

    Private Sub CustomerDataGridView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CustomerDataGridView.Click
        Me.Validate()
        Me.CustomerBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.Database2DataSet)
    End Sub


    Private Sub CustomerDataGridView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles CustomerDataGridView.CellContentClick

    End Sub

End Class
 
VB.NET:
CustomerDataGridView.DataSource = myDataSet.Tables("Customer").DefaultView

So you set your DataGridView to a completely new DataSource and you expect your bound textboxes to follow along?
 
Ok you have spotted what you reckon is the problem. What do I put instead of it to correct it? Can you provide some more information? Thanks for finding this Matt.
 
After testing more buttons, functions, etc on my form I have realised that after I perform a search the datagrid stops responding to any changes a button may. For example I filter the grid by using some queries being called from button clicks. All work fine. When I initiate a search all stops working. I have to close and re open the form to make it work. I am stuck guys. Any help is greatly appreciated
 
First off you're calling Me.CustomerTableAdapter.Fill twice in form load.

Create a new query in your data set called FillBySearchCriteria or something meaningful to you and call Me.CustomerTableAdapter.FillBySearchCriteria in the TextChanged event like you're doing with your AdHoc query.

Here's an example of filtering data in your query by using a toolstrip textbox. You should be able to easily modify it to search across your columns. How Do I: Create a Search Form?
 
Thanks for the replies Matt. I have already gone that way and I encountered other problems. The tutorial I have seen it, but cheers for the link. How about the exisiting code? I am completely stuck and I cannot get anywhere forward.
 
I could not go over the data and fill my datagrid with the results. I could make the search work. I could not make my queries to work and I had always an empty datagrid view. I did not know if it was the search fault or filling the grid.
 
Modification of the query in the video to search the FirstName and LastName columns for entries beginning with the search criteria.

VB.NET:
SELECT CustomerID, LastName, FirstName, Address, City, State, ZIP, Modified
FROM Customer
WHERE (LastName LIKE @Criteria + '%') OR (FirstName LIKE @Criteria + '%')

Running this using 'a' as a search term results in Bryan Aadams and Amanda Silver being returned with the sample database included in the project.
 
Have tried. First in the query builder when I press 'Execute query' I do not get the query parameter window to enter data to search my table as the video shows. Nevertheless I complete it and I get a toolstrip with only a button (filby) and no search area ( textbox ). When I press it the datagrid just goes empty. This is the best I could get from it. Same as the last times I was doing it. Followed every step exactly and even created a single form using northwind as database but no luck there either.

As my code is now there is nothing we can do to stop this problem happening? Any direction on where the problem resides will be great.
 
SELECT CustomerID, LastName, FirstName, Address, City, State, ZIP, Modified
FROM Customer
WHERE (LastName LIKE @Criteria + '%') OR (FirstName LIKE @Criteria + '%')

Avoid putting the wildcard in the query -> it is better to leave this flexibility to the client app or the user themselves
 
As my code is now there is nothing we can do to stop this problem happening? Any direction on where the problem resides will be great.

Please post the project you just made, and I'll fix it. Do NOT include the BIN or OBJ folders in your zip attachment
 
Here it is. It is just the modcustomer form files in a zip file. I have not included databases and rest of project as it is too big.
 

Attachments

  • epos.zip
    15.2 KB · Views: 16
Back
Top