Results 1 to 6 of 6

Thread: [RESOLVED] [02/03] Adding a row in a datatable

  1. #1

    Thread Starter
    Lively Member deien's Avatar
    Join Date
    Sep 2007
    Posts
    81

    Resolved [RESOLVED] [02/03] Adding a row in a datatable

    Hello Everyone!
    I have a datatable that contains rows of data.
    In what index exactly this command:
    Code:
    idt_DataTable.Rows.Add(lrw_NewRow)
    adds a row into a datatable?

    Because I tried to access each and every row of the datatable after i issue this command but it seems that the newly added row is not in it. How is this possible?

    Please help me.. Thank you so much.
    Deien369
    Computer Programmer
    Accent Micro Technologies, Inc.

    "The more grain a stalk has, the lower it bows; the more knowlege we recieve, the more humble we should become."

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Adding a row in a datatable

    It doesn't insert it. If you want to insert it then you'd use the Insert method. The Add method simply adds the new item to the end of the collection, as the Add method of any and all collections do. That means its index will be idt_DataTable.Rows.Count-1.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: [02/03] Adding a row in a datatable

    This procedure inserts an array of objects into a specified row index. You will need to ensure that the length of the passed array is the same as the number of columns in your data table and that the values are validated to the columns' data types.
    Code:
    Public Sub InsertRowIntoDataTable(ByVal myDataTable As DataTable, ByVal objValues As Object(), _
                    ByVal iIndex As Integer)
    
            Dim myRowItem As DataRow
            Dim i As Integer
    
            myRowItem = myDataTable.NewRow
    
            For i = 0 To objValues.GetUpperBound(0)
                myRowItem(i) = objValues(i)
            Next i
    
            myDataTable.Rows.InsertAt(myRowItem, iIndex)
    
        End Sub

  4. #4

    Thread Starter
    Lively Member deien's Avatar
    Join Date
    Sep 2007
    Posts
    81

    Re: [02/03] Adding a row in a datatable

    Thanks a lot guys for you help.
    But if you don't mind, i still have another question to ask.

    In deleting a row in a datatable.
    How come that this code:
    Code:
    icls_DBManagement.idt_DataTable.Rows(ii_RowIndex).Delete()
    and even this one:
    Code:
    icls_DBManagement.idt_DataTable.Rows.RemoveAt(ii_RowIndex)
    does not remove the row in the datatable?
    Because everytime that I add/insert a row to the index where the said row that i expect to be deleted, I get this exception msg:
    "This row already belongs to this table."

    Thanks.
    Deien369
    Computer Programmer
    Accent Micro Technologies, Inc.

    "The more grain a stalk has, the lower it bows; the more knowlege we recieve, the more humble we should become."

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Adding a row in a datatable

    That error message has nothing whatsoever to do with whether or not a row has been deleted. That error message is telling you that the DataRow object that you're trying to Add has already been added. You can't add the same row twice. When I say "the same row" I mean the same DataRow object, not two different DataRows with the same data.

    Now, let's look at the code you posted. There is a BIG difference between deleting a DataRow in a DataTable and removing it. When you remove a row from a DataTable it is simply taken out, like it was never added in the first place. That has no effect whatsoever on the database so there's no way the corresponding record is going to be deleted from the database.

    When you delete a row it is still in the Datatable but it is flagged as Deleted. When you then save the changes back to the database the corresponding record will be identified because it will have the same ID and it will be deleted. At that point the row will be implicitly removed from the DataTable as it's no longer required.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member deien's Avatar
    Join Date
    Sep 2007
    Posts
    81

    Re: [02/03] Adding a row in a datatable

    Hey! Your so cool men! You really helped me out there! Thanks a lot..
    Deien369
    Computer Programmer
    Accent Micro Technologies, Inc.

    "The more grain a stalk has, the lower it bows; the more knowlege we recieve, the more humble we should become."

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