Datagrid problem

kasteel

Well-known member
Joined
May 29, 2009
Messages
50
Programming Experience
10+
Hi

I am very new to using a datagrid and need some help if possible. :)

I added a Datasource in my project (Visul Studio 2005 - VB) which is connected to an Access Database.

When I make changes in the datagrid and press Save it saves and updates the info as expected.

A short while ago I added a search function with text boxes which will populate data in the same datagrid.

The problem is that when I search for a user and it displays the search result in the datagrid it won't accept any changes I make to the user's details even if I press Save. It just reverts back to the previous result which showed before doing the search.

Example:

This works: Editing a user's details in the datagrid + press save.

This does not work: Doing a search + Editing a user's details in the datagrid + press save.

Here is the code I use for doing the search:

VB.NET:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        Dim strAddressSearch As String
        Dim strAddressSearch2 As String
        strAddressSearch = txtSearch.Text
        strAddressSearch2 = txtSearch2.Text

        Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=C:\mydb.mdb")
        Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM Sheet1 WHERE LastName Like '%" & strAddressSearch & "%' and FirstName Like '%" & strAddressSearch2 & "%' ", con)


        con.Open()
        Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        Dim myDataSet As DataSet = New DataSet()
        myDA.Fill(myDataSet, "MyTable")
        Sheet1DataGridView.DataSource = myDataSet.Tables("MyTable").DefaultView

    End Sub

Any help would be appreciated :)
 
I think I fixed it.

Let me know if I am still wrong :)

I am now using this instead of the code I used above.

VB.NET:
 Sheet1DataGridView.DataSource = PgsaDataSet.Sheet1
        Dim strName As String
        strName = TextBox3.Text

        PgsaDataSet.Sheet1.DefaultView.RowFilter = String.Format("LastName Like '{0}%'", strName.Trim)
 
Your search is not filtering your original data but instead retrieving new data from the database and holding it within its own newly created dataset....
 
Thanks :)

I am now using this:

VB.NET:
Sheet1DataGridView.DataSource = PgsaDataSet.Sheet1
        Dim strName As String
        strName = TextBox3.Text

        PgsaDataSet.Sheet1.DefaultView.RowFilter = String.Format("LastName Like '{0}%'", strName.Trim)
 
Back
Top