locked
Adding multiple rows to a datatable RRS feed

  • Question

  • User933465580 posted

    I know I can add rows to a datatable one at a time using something like:

    Mydatatable.rows.add(myrow)

    However, how can I add multiple rows without putting the above code in a loop and doing the rows one at a time?

    I’d like to do something like

    Mydatatable.rows.add(another_datatable.select(foo=bar))

    Thus adding multiple rows with one call?

    Thanks

    Tuesday, June 21, 2011 4:23 AM

Answers

  • User551462331 posted

    how about getting dataview of dataset with filter condition and then cloning filtered dataview in another dataset

    dim FilteredTable as New DataTable();
    DataView dvData = new DataView(MyDataTable);
    dvData.RowFilter = "foo = bar";
    FilteredTable = dvData.Table.Clone();

    hope this helps...

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 21, 2011 4:49 AM

All replies

  • User-1188032534 posted

    You could use the DataAdapter to do the work. DataAdapter.Fill(DataTable) will append one or many rows to your DataTable.

    Take a look at this article: http://msdn.microsoft.com/en-us/library/bh8kx08z.aspx

    Regards 

    Tuesday, June 21, 2011 4:27 AM
  • User-1309402178 posted

    hi  mick0987b

    yes you are correct it will add rows at a time to the table in a single shot

    dt.Rows.Add(dt1.Select("fname=bob");

    B Bhupal

    if it is helpful mark it as a answer

    Tuesday, June 21, 2011 4:44 AM
  • User551462331 posted

    how about getting dataview of dataset with filter condition and then cloning filtered dataview in another dataset

    dim FilteredTable as New DataTable();
    DataView dvData = new DataView(MyDataTable);
    dvData.RowFilter = "foo = bar";
    FilteredTable = dvData.Table.Clone();

    hope this helps...

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 21, 2011 4:49 AM
  • User933465580 posted

    Hi B,

    I get an error when I try this.

    {"Input array is longer than the number of columns in this table."}

    my code:

    mydatatable.Rows.Add(branches.Select("mykey > 1"))

    ??

    Thanks

    Tuesday, June 21, 2011 5:05 AM
  • User-1188032534 posted

    That probably occurs because the braches result doesn't match the mydatatable structure. The number of columns and data types must mach eachother.

    Tuesday, June 21, 2011 5:23 AM