none
Getting a DataTable subset from a Dataset RRS feed

  • Question

  • Hi, this must be trivial but I cannot seem to get it...

     

    I have a DataSet with one DataTable and would like to Select or Filter the data from that one DataTable as I only want to display certain data. The Select method returns DataRow[] but I'd like to get another DataTable filled with the FILTERED data. This IS for display only...

     

    I tried adding the DataRow[] to a new table but cannot as C# gives me an error that the row(s) already belong to another table...

     

    Can someone please advise.

     

    In the end I still want a DataTable of the FILTERED data and not a DataRow[] or DataView etc etc. Happy to write a method around this but cannot seem to get it figured out.

     

    It cannot be that difficult and I'd like to know what I'm missing or not doing ....

     

    Help!

     

    Friday, October 24, 2008 6:09 PM

Answers

  • OK, figured it out... long winded but that is what we need.

     

                            DataTable dt;

                            DataViewManager dvm = _MyDataSet.DefaultViewManager;
                            dv = dvm.CreateDataView(_MyDataSet.Tables[0]);
                            dv.RowFilter = "somefield='data'";
                            dt = dv.ToTable();

     

    Now you can use THAT DataTable ...

     

    If anyone has a simpler way of returning a subset of data from the DataSet as a DataTable, please post here!

     

    thanks

    Friday, October 24, 2008 7:07 PM

All replies

  • OK, figured it out... long winded but that is what we need.

     

                            DataTable dt;

                            DataViewManager dvm = _MyDataSet.DefaultViewManager;
                            dv = dvm.CreateDataView(_MyDataSet.Tables[0]);
                            dv.RowFilter = "somefield='data'";
                            dt = dv.ToTable();

     

    Now you can use THAT DataTable ...

     

    If anyone has a simpler way of returning a subset of data from the DataSet as a DataTable, please post here!

     

    thanks

    Friday, October 24, 2008 7:07 PM
  • You don't need the DataViewManager, you can just use 'new DataView'

    Code Snippet
    DataTable dt = (new DataView(table, "somefield='data'", "", DataRowViews.CurrentRows).ToTable();

    or

    Code Snippet

    DataTable dt = table.Clone();

    foreach(DataRow row in table.Select("somefield='data'"))

    {

        dt.ImportRow(row);

    }

     

     

     

     

    Friday, October 24, 2008 10:56 PM
    Moderator