Visual Basic > Visual Basic Forums > Visual Basic Language > Vb.net 2008 Problem adding a datarow to a datatable
Ask a questionAsk a question
 

AnswerVb.net 2008 Problem adding a datarow to a datatable

  • Wednesday, November 04, 2009 7:31 PMRobtyketto Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    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

  • Friday, November 06, 2009 10:46 AMJoao S Cardoso Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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.
  • Friday, November 06, 2009 1:50 PMMalange Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    you would or could avoid all this if you just use BindingSource.....
    Don't judge me, just Upgrade me. Thanks!
  • Friday, November 06, 2009 1:51 PMRobtyketto Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    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)

      '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
    
    Thanks
    Rob
    • Marked As Answer byRobtyketto Monday, November 16, 2009 10:05 AM
    •  

All Replies

  • Friday, November 06, 2009 10:46 AMJoao S Cardoso Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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.
  • Friday, November 06, 2009 1:50 PMMalange Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    you would or could avoid all this if you just use BindingSource.....
    Don't judge me, just Upgrade me. Thanks!
  • Friday, November 06, 2009 1:51 PMRobtyketto Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    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)

      '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
    
    Thanks
    Rob
    • Marked As Answer byRobtyketto Monday, November 16, 2009 10:05 AM
    •  
  • Friday, November 06, 2009 3:59 PMRobtyketto Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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?


  • Friday, November 06, 2009 11:07 PMMalange Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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!
  • Saturday, November 07, 2009 1:38 PMRobtyketto Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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!
  • Tuesday, November 10, 2009 8:01 AMMaDFroG20091013 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yes, they both bind data. And they are same essentially.