Re: Add array of rows to DataTable

Discussion in 'ASP .Net' started by S. Justin Gengo, Jul 15, 2003.

  1. Frank,

    I don't know of any way to add an array of rows. I believe that you'll have
    to iterate through the array and add each one at a time:

    DataTable.Rows.Add(SingleDataRow)

    But to clear all rows is pretty easy:

    DataTable.Rows.Clear()

    Justin


    "frank" <> wrote in message
    news:...
    > How do I add an array of DataRows (i.e. DataRow[]) to a DataTable?
    >
    > Also, how do I remove all rows from a DataTable?
    >
    >
     
    S. Justin Gengo, Jul 15, 2003
    #1
    1. Advertising

  2. S. Justin Gengo

    frank Guest

    "S. Justin Gengo" <> wrote in message
    news:...
    > "frank" <> wrote in message
    > news:...
    > > How do I add an array of DataRows (i.e. DataRow[]) to a DataTable?
    > >
    > > Also, how do I remove all rows from a DataTable?
    > >
    > >

    > Frank,
    >
    > I don't know of any way to add an array of rows. I believe that you'll

    have
    > to iterate through the array and add each one at a time:
    >
    > DataTable.Rows.Add(SingleDataRow)


    OK, I've done that but now it says "This row already belongs to another
    table". I know you can't move a DataRow from one collection to another if it
    belongs to another - so I tried dtFiltered.ImportRow(dr).

    When I do it that way, it appears to add the rows without any problem, but
    when it comes to actually referencing and using the data later on.

    If I try:

    Console.Write(dr[0].ToString() + "\r\n")

    It says "Cannot find column 0".

    Any ideas?

    > But to clear all rows is pretty easy:
    >
    > DataTable.Rows.Clear()


    I tried this to see if it would let me use the Add method, but it didn't
    work.
     
    frank, Jul 15, 2003
    #2
    1. Advertising

  3. Frank,

    If you're referring to the row that was just added to the table then the row
    object might get cleared when it's imported. That's my best guess.

    Can you refer to it in full? (i.e. DataTableImportedTo.Rows(0)(0).ToString)

    Sorry, but I don't know the equivilant in C#. It should be pretty close to
    the same though...

    Justin


    "frank" <> wrote in message
    news:%...
    > "S. Justin Gengo" <> wrote in message
    > news:...
    > > "frank" <> wrote in message
    > > news:...
    > > > How do I add an array of DataRows (i.e. DataRow[]) to a DataTable?
    > > >
    > > > Also, how do I remove all rows from a DataTable?
    > > >
    > > >

    > > Frank,
    > >
    > > I don't know of any way to add an array of rows. I believe that you'll

    > have
    > > to iterate through the array and add each one at a time:
    > >
    > > DataTable.Rows.Add(SingleDataRow)

    >
    > OK, I've done that but now it says "This row already belongs to another
    > table". I know you can't move a DataRow from one collection to another if

    it
    > belongs to another - so I tried dtFiltered.ImportRow(dr).
    >
    > When I do it that way, it appears to add the rows without any problem, but
    > when it comes to actually referencing and using the data later on.
    >
    > If I try:
    >
    > Console.Write(dr[0].ToString() + "\r\n")
    >
    > It says "Cannot find column 0".
    >
    > Any ideas?
    >
    > > But to clear all rows is pretty easy:
    > >
    > > DataTable.Rows.Clear()

    >
    > I tried this to see if it would let me use the Add method, but it didn't
    > work.
    >
    >
     
    S. Justin Gengo, Jul 15, 2003
    #3
  4. S. Justin Gengo

    frank Guest

    "S. Justin Gengo" <> wrote in message
    news:...
    > "frank" <> wrote in message
    > news:%...
    > > "S. Justin Gengo" <> wrote in message
    > > news:...
    > > > "frank" <> wrote in message
    > > > news:...
    > > > > How do I add an array of DataRows (i.e. DataRow[]) to a DataTable?
    > > > >
    > > > > Also, how do I remove all rows from a DataTable?
    > > > >
    > > > >
    > > > Frank,
    > > >
    > > > I don't know of any way to add an array of rows. I believe that

    you'll
    > > have
    > > > to iterate through the array and add each one at a time:
    > > >
    > > > DataTable.Rows.Add(SingleDataRow)

    > >
    > > OK, I've done that but now it says "This row already belongs to another
    > > table". I know you can't move a DataRow from one collection to another

    if
    > it
    > > belongs to another - so I tried dtFiltered.ImportRow(dr).
    > >
    > > When I do it that way, it appears to add the rows without any problem,

    but
    > > when it comes to actually referencing and using the data later on.
    > >
    > > If I try:
    > >
    > > Console.Write(dr[0].ToString() + "\r\n")
    > >
    > > It says "Cannot find column 0".
    > >
    > > Any ideas?
    > >
    > > > But to clear all rows is pretty easy:
    > > >
    > > > DataTable.Rows.Clear()

    > >
    > > I tried this to see if it would let me use the Add method, but it didn't
    > > work.
    > >

    >
    > If you're referring to the row that was just added to the table then the

    row
    > object might get cleared when it's imported. That's my best guess.
    >
    > Can you refer to it in full? (i.e.

    DataTableImportedTo.Rows(0)(0).ToString)
    >
    > Sorry, but I don't know the equivilant in C#. It should be pretty close

    to
    > the same though...


    Thanks for the help, but I found and solved the problem.

    For archiving purposes, here's what it was:

    When I used the ImportRow method it doesn't actually create any DataColumns
    in the DataTable and as DataColumns are required to reference the data (even
    though it *was* there) I could actually access it.

    I resolved the issue by creating the DataColumns first, before using
    ImportRow.
     
    frank, Jul 15, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J. Babe
    Replies:
    1
    Views:
    8,356
    Kevin Spencer
    Aug 15, 2003
  2. Ersin Gençtürk
    Replies:
    1
    Views:
    3,491
    Ersin Gençtürk
    Oct 6, 2004
  3. Craig G
    Replies:
    2
    Views:
    1,425
    Craig G
    Mar 7, 2005
  4. =?Utf-8?B?QW5kcmV3?=

    add new rows of data to a datatable of a dataset

    =?Utf-8?B?QW5kcmV3?=, Mar 22, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    37,366
  5. srinivas
    Replies:
    1
    Views:
    604
    Eliyahu Goldin
    Jan 17, 2007
Loading...

Share This Page