Add array of rows to DataTable

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Re: Add array of rows to 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)

    But to clear all rows is pretty easy:

    DataTable.Rows.Clear()

    Justin


    "frank" <frank@frank.com> wrote in message
    news:uvZOEEuSDHA.872@TK2MSFTNGP12.phx.gbl...
    > 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 Guest

  2. Similar Questions and Discussions

    1. How to retrieve first 50 rows , next 50 rows and so on in Informix
      Hi All Is there any equivalent command for SET ROWCOUNT or LIMIT in INFORMIX to get the first 50rows, next 50 rows and so on if query returns...
    2. Modify rows in DataTable
      Hi Steve, Suppose you want to change text in some column depending on the data in data source, you can do it in ItemDataBound event. Dim...
    3. DataTable from an array of objects returned from a webservice
      I have the "simple" problem of wanting to take an array of objects I get from a webservice and turning them into a DataTable. The following is my...
    4. Select rows where other related rows don't exist
      I would like to select rows from a table where another related row in the same table doesn't exist and the relation key is more than 1 column. ...
    5. comparing rows in a multidimensional array
      how can i compare rows (3 or more rows) in a multidiemensional array? they all contain numeric values, i need to find its similar values and unique...
  3. #2

    Default Re: Add array of rows to DataTable

    "S. Justin Gengo" <gengoj@krause.com> wrote in message
    news:vh86nleiv5f01a@corp.supernews.com...
    > "frank" <frank@frank.com> wrote in message
    > news:uvZOEEuSDHA.872@TK2MSFTNGP12.phx.gbl...
    > > 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 Guest

  4. #3

    Default Re: Add array of rows to DataTable

    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" <frank@frank.com> wrote in message
    news:%23INi8XuSDHA.1552@TK2MSFTNGP10.phx.gbl...
    > "S. Justin Gengo" <gengoj@krause.com> wrote in message
    > news:vh86nleiv5f01a@corp.supernews.com...
    > > "frank" <frank@frank.com> wrote in message
    > > news:uvZOEEuSDHA.872@TK2MSFTNGP12.phx.gbl...
    > > > 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 Guest

  5. #4

    Default Re: Add array of rows to DataTable

    "S. Justin Gengo" <gengoj@krause.com> wrote in message
    news:vh88d7ab0hpne0@corp.supernews.com...
    > "frank" <frank@frank.com> wrote in message
    > news:%23INi8XuSDHA.1552@TK2MSFTNGP10.phx.gbl...
    > > "S. Justin Gengo" <gengoj@krause.com> wrote in message
    > > news:vh86nleiv5f01a@corp.supernews.com...
    > > > "frank" <frank@frank.com> wrote in message
    > > > news:uvZOEEuSDHA.872@TK2MSFTNGP12.phx.gbl...
    > > > > 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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139