Visual Basic >
Visual Basic Forums
>
Visual Basic Language
>
Vb.net 2008 Problem adding a datarow to a datatable
Vb.net 2008 Problem adding a datarow to a datatable
- Greetings,
See the code below which is triggered on a datagridview row validation event where the datagridview datasource is a datatable.
The messagebox shows that it loops around as expected populating the datarow with the expected values (NOTE: There are no primary keys as this is a college assignment with specific requirement and all fields can be nullable).
I had to use ImportRows as AddRows would always error due to "This row belongs to another table" the odd thing was the values were different and again the table contains no primary keys.
The code below runs but never adds a datarow to the dataset datatable "Bookings", does anyone know what the problem might be?
Private Sub DataGridView1_RowValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowValidated Dim currentRowToValidate As DataGridViewRow = DataGridView1.Rows(e.RowIndex) If currentRowToValidate.Cells("bookingsFrequency").Value > 1 Then Dim dsNewBooking As DataRow = bookingsdata.Tables("bookings").NewRow() Dim daystoadd = 7 'Add number of bookings per value entered in frequency For i As Integer = 1 To currentRowToValidate.Cells("bookingsFrequency").Value '---create a row--- ' Dim newBooking As New DataGridViewRow dsNewBooking.Item(0) = currentRowToValidate.Cells("bookingDate").Value.AddDays(daystoadd) dsNewBooking.Item(1) = CType(currentRowToValidate.Cells("startTime").Value, DateTime) dsNewBooking.Item(2) = CType(currentRowToValidate.Cells("endTime").Value, DateTime) dsNewBooking.Item(3) = "Value 1" dsNewBooking.Item(4) = "Value 2" dsNewBooking.Item(5) = "Value 3" bookingsdata.Tables("bookings").ImportRow(dsNewBooking) MessageBox.Show("Num of rows = " & bookingsdata.Tables("bookings").Rows.Count) daystoadd += 7 Next End If End Sub
Thanks
Rob
Answers
Are you doing an AcceptChanges somewhere else? If not, there is your problem.
http://msdn.microsoft.com/en-us/library/system.data.dataset.acceptchanges(VS.71).aspx
Please remember to mark the replies as answers if they help you.- Marked As Answer byJeff ShanMSFT, ModeratorWednesday, November 11, 2009 5:20 AM
- you would or could avoid all this if you just use BindingSource.....
Don't judge me, just Upgrade me. Thanks!- Marked As Answer byJeff ShanMSFT, ModeratorWednesday, November 11, 2009 5:20 AM
- Thanks for the reply, as I was creating an instance of a datarow for a specified dataset datatable, then populating the row with column values and importing back to the SAME dataset datatable I believe I wasn't using the importrow statement as intended (copying from one datatable to another).
In the end I used addedrows (which when I initially attempted to use error'd with duplicate row added, can't remember how I fixed the issue though)
Thanks'Add number of bookings per value entered in frequency For i As Integer = 1 To CType(currentRowToValidate.Cells("bookingsFrequency").Value, Integer) '---create a row--- Dim dsNewBooking As DataRow = bookingsdata.Tables("bookings").NewRow() ' Populate column details based on the newly added row dsNewBooking.Item(0) = CType(currentRowToValidate.Cells("bookingDate").Value, DateTime).AddDays(daystoadd) dsNewBooking.Item(1) = CType(currentRowToValidate.Cells("startTime").Value, DateTime) dsNewBooking.Item(2) = CType(currentRowToValidate.Cells("endTime").Value, DateTime) dsNewBooking.Item(3) = currentRowToValidate.Cells("activity").Value dsNewBooking.Item(4) = bookersName dsNewBooking.Item(5) = phoneNum daystoadd += 7 bookingsdata.Tables("bookings").Rows.Add(dsNewBooking) Next
Rob- Marked As Answer byRobtyketto Monday, November 16, 2009 10:05 AM
All Replies
Are you doing an AcceptChanges somewhere else? If not, there is your problem.
http://msdn.microsoft.com/en-us/library/system.data.dataset.acceptchanges(VS.71).aspx
Please remember to mark the replies as answers if they help you.- Marked As Answer byJeff ShanMSFT, ModeratorWednesday, November 11, 2009 5:20 AM
- you would or could avoid all this if you just use BindingSource.....
Don't judge me, just Upgrade me. Thanks!- Marked As Answer byJeff ShanMSFT, ModeratorWednesday, November 11, 2009 5:20 AM
- Thanks for the reply, as I was creating an instance of a datarow for a specified dataset datatable, then populating the row with column values and importing back to the SAME dataset datatable I believe I wasn't using the importrow statement as intended (copying from one datatable to another).
In the end I used addedrows (which when I initially attempted to use error'd with duplicate row added, can't remember how I fixed the issue though)
Thanks'Add number of bookings per value entered in frequency For i As Integer = 1 To CType(currentRowToValidate.Cells("bookingsFrequency").Value, Integer) '---create a row--- Dim dsNewBooking As DataRow = bookingsdata.Tables("bookings").NewRow() ' Populate column details based on the newly added row dsNewBooking.Item(0) = CType(currentRowToValidate.Cells("bookingDate").Value, DateTime).AddDays(daystoadd) dsNewBooking.Item(1) = CType(currentRowToValidate.Cells("startTime").Value, DateTime) dsNewBooking.Item(2) = CType(currentRowToValidate.Cells("endTime").Value, DateTime) dsNewBooking.Item(3) = currentRowToValidate.Cells("activity").Value dsNewBooking.Item(4) = bookersName dsNewBooking.Item(5) = phoneNum daystoadd += 7 bookingsdata.Tables("bookings").Rows.Add(dsNewBooking) Next
Rob- Marked As Answer byRobtyketto Monday, November 16, 2009 10:05 AM
you would or could avoid all this if you just use BindingSource.....
Don't judge me, just Upgrade me. Thanks!
I need to manually create rows based on the values of the currently added row, could a BindSource help in this situation?
you would or could avoid all this if you just use BindingSource.....
Don't judge me, just Upgrade me. Thanks!
I need to manually create rows based on the values of the currently added row, could a BindSource help in this situation?
so to speak yes.
bidingsource.addnew()
whatever you wnat to do
Don't judge me, just Upgrade me. Thanks!you would or could avoid all this if you just use BindingSource.....
Don't judge me, just Upgrade me. Thanks!
I need to manually create rows based on the values of the currently added row, could a BindSource help in this situation?
so to speak yes.
bidingsource.addnew()
whatever you wnat to do
Don't judge me, just Upgrade me. Thanks!
Im quite new to vb.net and datasource and bindingsource often confuse me in their intentions, as don't they both bind data?
What is the main difference?
Hope that isn't too stupid a question to ask!- Yes, they both bind data. And they are same essentially.


