Results 1 to 5 of 5

Thread: how to update datasource from datagrid?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Location
    Europe, Belgium
    Posts
    84

    Question how to update datasource from datagrid?

    Me again,

    I have some problems for updating a datasource from a datagrid.

    My form has a dataset with 2 tables (tblCD_Detail and tblCD) which have a master detail relationship. There is also a datagrid (datagrid1) which displays the tblCD_detail fields corresponding with the selected tblCD.

    The user can make changes in the datagrid, but the problem is how to update the corresponding datasource when the form is closed. Herefore I wrote the following code:


    Private Sub test_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    If dataset.HasChanges(DataRowState.Modified) Then
    Dim commandbuilder As OleDbCommandBuilder

    commandbuilder = New OleDbCommandBuilder(DACD_detail)
    DACD_detail.Update(dataset, "tblCD_detail")

    End If
    end sub


    I use the commandbuilder because I wrote the dataadapter myself (I didn't use the wizard), I also used the same dataadapter as the one I used to create the datatable tblCD_detail.

    When I run the form, I don't get an error but my database isn't updated. Does anybody know what I'm doing wrong???

    Has it something to do with the relationship between the tables?

    Thanx,

    Tom

  2. #2
    Addicted Member Hole-In-One's Avatar
    Join Date
    Mar 2003
    Location
    Minnesota
    Posts
    195
    All I know is that when I try to use Relationships between tables all it does is cause headaches!!
    Has it something to do with the relationship between the tables?
    So my answer would be yes

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Location
    Europe, Belgium
    Posts
    84
    thanks,

    So, it would be good to post my code where I create the relation; here it is


    SQLCD = "SELECT * FROM tblCD"
    SQLCD_detail = "SELECT * FROM tblCD_detail"
    DACD = New OleDbDataAdapter(SQLCD, connection)
    DACD_detail = New OleDbDataAdapter(SQLCD_detail, connection)
    connection.Open()
    DACD.Fill(dataset, "tblCD")
    DACD_detail.Fill(dataset, "tblCD_detail")
    connection.Close()

    myDataRelation = New DataRelation("tblCDtblCD_detail", dataset.Tables("tblCD").Columns("CDID"), dataset.Tables("tblCD_detail").Columns("CDID"))
    dataset.Relations.Add(myDataRelation)

    Me.DataGrid1.SetDataBinding(dataset, "tblCD.tblCDtblCD_detail")


    I think the most important line is the last green one, where I set my databindings.

    Can anybody help me please, I'm stuck.

    Tom

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    On how to get Relation part working , see this example .
    http://www.vbforums.com/showthread.p...hreadid=253655

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2003
    Location
    Europe, Belgium
    Posts
    84
    Thanks Pirate,

    I checked your code and I understand it, I used the same to code my dataset and datarelation. So, I think the problem has to be in the update of the datasource from the datagrid.

    I simplified the application to get to the essence of the problem; now I have:
    - a dataset
    - a dataadapter to fill the dataset
    - a datagrid with the dataset as datasource
    - an update button to write the changes in the datagrid to the datasource.

    When the users adds a row in the datagrid, then these changes are not send back to the database. I don't understand this because I don't get error messages.

    The code is added inthe attachment, so you can take a look if you want.

    Thanks,

    Tom
    Attached Files Attached Files

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