Results 1 to 7 of 7

Thread: Textbox to Datagridview to Access Database not updating

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    20

    Question Textbox to Datagridview to Access Database not updating

    Hi guys,

    Looking for some help with this one.

    I have an access database which is being read into a datagrid view. When I type directly into the datagridview and press "Save" it saves perfectly. But when I edit by passing the field values to another form to be edited and then pass them back to the datagrid view, they won't save at all. They look like they've been saved and can be seen in the DGV but when I restart the program they are gone.

    As my save procedure works when I input directly into a DGV it must be how I'm passing the values back to the DGV that they are not saving. They don't seem to be "binding" to the DGV once passed back.

    Here's how I pass them:

    Code:
    'Passing to the edit form
    
    Dim indexPos As Integer = dgvStudents.CurrentCell.RowIndex
             frmEdit.txtEditRecordID.Text = Me.dgvStudents.Item("StudentID", indexPos).Value
    
             frmEdit.Show()
             Me.Hide()
                
    
    
    'Passing back from the edit form to the DGV:
    
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    
            Form3.SaveFromEdit()
            Me.Close()
    
    
        End Sub
    
    Public Sub SaveFromEdit()
            Dim indexPos As Integer = dgvStudents.CurrentCell.RowIndex
    
            Me.dgvStudents.Item("student_IDNumber", indexPos).Value = frmEdit.txtEditStudentNumber.Text
    
    
    
            Save()
    
        End Sub
    
    Public Sub Save()
            Me.Validate()
            Me.objStudentDA.Update(Me.objDataSet.Tables("tblStudent"))
            Me.objDataSet.AcceptChanges()
    
        End Sub
    Any help would be greatly appreciated!

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Textbox to Datagridview to Access Database not updating

    They don't seem to be "binding" to the DGV once passed back.
    Correct. Because it's not registered as an edit, assigning values directly to dgv cells will not activate binding. You should assign the new values to the datatable and allow the binding to update the dgv in this case.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    20

    Re: Textbox to Datagridview to Access Database not updating

    Quote Originally Posted by dunfiddlin View Post
    Correct. Because it's not registered as an edit, assigning values directly to dgv cells will not activate binding. You should assign the new values to the datatable and allow the binding to update the dgv in this case.
    Thanks dunfiddlin!

    How do I assign them to the datatable? When you say datatable do you mean database? Or is there a datatable for the the dgv?

    Any advice would be really appreciated as I'm new to this and have been searching online for ages.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Textbox to Datagridview to Access Database not updating

    I'm assuming that the table Me.objDataSet.Tables("tblStudent") is the binding source so whatever you were doing to the datagridview, you simply do to that table instead. If you need some assistance with that, start with MSDN's tutorials.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    20

    Question Re: Textbox to Datagridview to Access Database not updating

    Quote Originally Posted by dunfiddlin View Post
    I'm assuming that the table Me.objDataSet.Tables("tblStudent") is the binding source so whatever you were doing to the datagridview, you simply do to that table instead. If you need some assistance with that, start with MSDN's tutorials.
    Thanks!

    I read them but it doesn't explain why I can save them manually from within the DGV to the database but when I change the DGV from text boxes it won't save. Why is this?

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Textbox to Datagridview to Access Database not updating

    As I said, the DGV must register changes as an edit to affect the bound table. Simply changing a cell value does not count. Now you can do it in code by locating the cell and calling the BeginEdit method but it's a tedious and occasionally tricky process so it's invariably better to simply change the datatable directly.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    20

    Cool Re: Textbox to Datagridview to Access Database not updating

    Quote Originally Posted by dunfiddlin View Post
    As I said, the DGV must register changes as an edit to affect the bound table. Simply changing a cell value does not count. Now you can do it in code by locating the cell and calling the BeginEdit method but it's a tedious and occasionally tricky process so it's invariably better to simply change the datatable directly.
    Thanks! Got it working.

Tags for this Thread

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