locked
Add a DataRowView to a DataTable RRS feed

  • Question

  • Hi, I sorted a DataView created from a DataTable and then need to pull out some rows from the DateView to a different DataTable..

    or in code:

    DataTable MyDataTable = new DataTable ()
    DataView MyDataView = new DataView ( MyDataTable); MyDataView.Sort("ColumnX"); //work out a magic i MyAnotherDataTable.Rows.Add(MyDataView[i]);
    This gives an exception: "Input array is longer than the number of columns in this table"

    Can anyone help pointing me to the right direction, thanks in advance.

    Cheers,
    Dan

    Life is simple.
    Tuesday, March 16, 2010 5:49 PM

Answers

  • Try   DataTable myAnotherDatatable = MyDatatable.Clone();

    and then try Importing the Rows or Adding rows. It should avoid any issue of different schema.

    • Marked as answer by Firen Wednesday, March 17, 2010 9:25 AM
    Tuesday, March 16, 2010 6:13 PM

All replies

  • the reason could be MyAnotherDataTable does not have the same schema as MyDataTable..then the question is..how to copy the schema across different DataTables (but not the data)..


    Life is simple.
    Tuesday, March 16, 2010 6:10 PM
  • Try   DataTable myAnotherDatatable = MyDatatable.Clone();

    and then try Importing the Rows or Adding rows. It should avoid any issue of different schema.

    • Marked as answer by Firen Wednesday, March 17, 2010 9:25 AM
    Tuesday, March 16, 2010 6:13 PM
  • Thanks mate, .Clone() works great
    Life is simple.
    Wednesday, March 17, 2010 9:25 AM
  • DataTable myAnotherDatatable = MyDatatable.Copy();

    will copy the schema and the data.
    Wednesday, March 17, 2010 4:34 PM