DataGridView adding data Problem

quick5pnt0

Member
Joined
Oct 1, 2008
Messages
9
Programming Experience
Beginner
I'm a complete newbie to this so if anyone can help it would be greatly appreciated.

I watched the Databinding video on msdn (here) and got a DataGridView working like the video said. However I'm trying to go from there and programatically enter the information into the DataGridView. However I'm completely stumped as to why it wont work.

You can view my code below for entering the data. Currently I can still manually enter the data into the grid and click this button and it'll save it to the database, however it won't add it programatically. Like I said I'm a complete rookie so I may be way off. Can anyone help?

EDIT: More information in the post below.

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

            Numberofrow = Database1DataSet.Tables("times").Rows.Count + 1
            row = Database1DataSet.Tables("times").NewRow
            row(0) = Database1DataSet.Tables("times").Rows.Count + 1
            row(1) = TextBox1.Text
            row(2) = DateTime.Now
            row(3) = Label1.Text
            Database1DataSet.Tables("times").Rows.InsertAt(row, Numberofrow)
            Database1DataSet.Tables("times").AcceptChanges()
            TimesBindingSource.EndEdit()

            Dim rowsaffected As Integer
            TimesTableAdapter.Update(Database1DataSet.times)

            MessageBox.Show(rowsaffected.ToString)

    End Sub
 
Last edited:
I just re-read what I wrote above and I'm not sure I was completely clear. I got it so that I could manually enter the information straight into the datagrid and click that button and it would save it to the database. However I can't figure out the next step which is programatically entering the information and having it save to the database.

To put it simply: With that code it enters the data into the datagrid but doesn't save to the database. Anyone know why? Is the code completely off?
 
The DataAdapter.Update calls the respective INSERT, UPDATE, or DELETE statements for each inserted (Added), updated (Modified), or deleted (Deleted) row in your specified DataSet.

When you enter a new record to your dataset/table, it has a rowstate property of "Added ". When you call AcceptChanges on the DataSet, the RowState property of each DataRow also changes; Added and Modified rows become Unchanged.

Since you have already acceptedchanges and set all rows to Unchaged there is nothing new for the dataadapter to send to the database when you call its update.
 
Last edited:
I'm a complete newbie to this so if anyone can help it would be greatly appreciated.

I'll edit your code so it is better

VB.NET:
Private Sub GIVE_THIS_BUTTON_A_PROPER_NAME_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GIVE_THIS_BUTTON_A_PROPER_NAME.Click
            --if Numberofrow is a class wide variable, make it a local one. 
            --Dim it and give it a type. give your dataset a proper name, not just BlahBlah1
            --datasets are strongly typed for a reason. Don't use .Tables(string) to access tables. Use the named property
            [B]Dim rowNum [/B][B]as Integer [/B]= [I]Database1DataSet[/I].[B]times[/B].Rows.Count + 1
            --again, dim your variables and give them a type. try turning on Option Explicit and Option Strict. It will make you a better programmer
            [B]Dim row as Database1DataSet.timesTable[/B] = Database1DataSet.[B]timesTable[/B].[B]NewtimesTableRow()[/B]
            --now that row is a strongly typed variable, it has named properties for each column. use them
            --please tell me youre NOT using rowCount+1 as an ID!
            --why bother initialising an integer before, with the RowCount+1 if youre gonna repeat that here? use rowNum from above!
            row.[B]ID [/B]= [B]rowNum[/B]
            --GIVE YOUR CONTROLS PROPER NAMES, even in example code for forums. ESPECIALLY in example code for forums
            row.[B]PersonName [/B]= [B]personNameTextbox[/B].Text --TextBox1.Text
            row.[B]DateOfBirth [/B]= DateTime.Now
            row.[B]SomethingElse [/B]= Label1.Text
            --changed so much, i cant even comment
            Database1DataSet.[B]times.AddtimesTableRow(row)[/B]
            --dont do this
            Database1DataSet.Tables("times").AcceptChanges()
            --well you didnt really use the BS so no need to endedit on it
            TimesBindingSource.EndEdit()

            -- you dim this variable then don't use it, so its gonna default to 0 and NEVER be updated (just because you fire off a db update on the next 
            --line doesnt mean VB will intelligently work out this is where you wanted to store the result
            Dim rowsaffected As Integer
 
            --good
            TimesTableAdapter.Update(Database1DataSet.times)

            --mm, rowsaffected is at this point, set to 0?
            MessageBox.Show(rowsaffected.ToString)

    End Sub
 

Latest posts

Back
Top