Question Two probs with datagridview

Super_Grover

Active member
Joined
Apr 7, 2010
Messages
32
Programming Experience
Beginner
Hi all,
I'm struggling with two problems involving datagridview and an access databse. I'm working with VS2008 (VB2008) which seems to differ quite a bit from 2005 on this subject. All I could find on the internet involved mainly VB2005 and didn't work for me.

Problem 1:
Saving new row to db. I have the following code. Row is added in the DGV (and dataset I guess), but is not written for real to the db. Even if I close the appl and reopen the row is still there, but after a couple of minutes it appears not to be. Opening the db directly in Access show that the new data is not saved. Any ideas guys?

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Add.Click
        Dim newToetsingRow As DataRow = SampleRegDataSet.Tables("tbl_toetsingen").NewRow()
        Dim li_aantal As Integer = Convert.ToInt32(mtxt_aantal.Text)
        Dim li_akkoord As Integer = Convert.ToInt32(mtxt_akkoord.Text)
        Dim li_betaald As Integer = Convert.ToInt32(mtxt_betaald.Text)
        Dim ls_opm As String = mtxt_opm.Text
        Dim ld_datum As Date = dtp_datum.Value.Date
        Dim li_totaalbedrag As Integer = Convert.ToInt32(mtxt_aantal.Text) * 25
        newToetsingRow("User_ID") = gi_iser
        newToetsingRow("Datum") = ld_datum
        newToetsingRow("Aantal_toetsingen") = li_aantal
        newToetsingRow("Aantal_akkoord") = li_akkoord
        newToetsingRow("Aantal_betaald") = li_betaald
        newToetsingRow("Totaal_bedrag") = li_totaalbedrag
        newToetsingRow("Opmerkingen") = ls_opm

        SampleRegDataSet.Tables("tbl_toetsingen").Rows.Add(newToetsingRow)
        Me.Tbl_toetsingenTableAdapter.Update(Me.SampleRegDataSet)
        MsgBox("New row added!")
End Sub


Problem two:
I would like to use different queries on this DGV from the same db. So, say a couple of buttons which represent another selection of the db. I tried defining another dataAdapter which holds another query, but can't figure out how to display other data in the DGV than the bound data in the first dataset.
Any help is appreciated!
 
Update can't return False. It returns a number.

How did you create and bind the grid in the first place? Did you do it all manually or did you drag a table from the Data Sources window?
 
I created a boolean and put the update result in it. Sorry, my bad. So when I make it an integer it returns an '0'.

I created a very simple form, dragged the table (datagridview) from the datasource explorer and also a textfield/datefield for every database item (also drag-n-drop). Further a 'Add' button (which works fine) and an update button (which I'm struggling with).
At this moment the database consists of 3 tables, but on this form only one is used. This table has 8 colomns.
So, a have a dgv with 8 colomns, 8 datafields (which correspond with the dgv colomns), a dataset, a bindingSource, a dataTableAdapter and a TableAdapterManager.
From my books I can understand the idea behind the different components. Dataset contains an offline 'image' of the database, Dataset holds different DataTables which contain the actual data (?), TableAdapter which interacts between the dataset and database. Bindingsource and TableAdapterManager I'm not really sure about...
But it's very abstract at this point and I'm trying to bring it to life for myself. But I have to it working I guess to understand it.
From your answers I try step-by-step but to get the right syntax is hard at this point.
 
BTW: I noticed a couple of links in your sig, which seem very usefull. I am at work now and tonight (which differs because I'm located in Europe) I'll try to read all this. I looks like it is what I'm looking for....
 
Well, I think I'm getting close now...
I tried this:

VB.NET:
 li_tblIndex = EPaysDataGridView.CurrentRow.Index
        EPaysDataSet.EPays(li_tblIndex).Ontv_bedrag = li_ontBed
        EPaysDataSet.EPays(li_tblIndex).Datum_bet = ld_Bet_dat
        If EPaysDataSet.HasChanges() Then
            MsgBox("Yes")
        Else
            MsgBox("No")
        End If
        Me.EPaysTableAdapter.Update(Me.EPaysDataSet.EPays)

The HasChanges() method return 'yes', so I guess that's a good thing.
The new values are added to the grid, but not saved for real to the database. Looking for that now among your links, but can't really find it for now.

btw: really great links in your sig! Clear and well structured small bits of code.

btw: how do I know if my dataSet is signed or unsigned?
 
So then, the data is being saved. If you can't see it then you're looking in the wrong place or at the wrong time. Follow the first link in my signature to learn how local data files are managed.
 
So then, the data is being saved. If you can't see it then you're looking in the wrong place or at the wrong time. Follow the first link in my signature to learn how local data files are managed.

Thank you again.
I've read this and we discussed this on page 1 of this thread.
So I changed the 'Copy always' property, but no changes. Then I build and published my appl but no data is being saved.

I have two projects: a testproject in which I try and try and try....to avoid pollution in the other, real, main project. When a function works in test I code the same in the main project.

Strange thing is: in my testproject 'HasChanges' return yes and 'Update' returns 1. I use the exact same code in the mainproject but the results are 'no' and '0'. In both case though, the data is not being saved to the access db.
 
Back
Top