locked
How to Filter and Sort a Strongly Typed DataSet to get a Strongly Typed DataSet Result RRS feed

  • Question

  • I need to get a strongly typed datatable that's a filtered and sorted subset of an original strongly typed datatable.  I've been trying various means, but can't seem to get a strongly typed dataset result.  I can get an untyped result dataset using DataView, but that's not what I want.  How can I do this.

    Thanks. 


    Thursday, May 24, 2012 9:26 PM

Answers

  • A data set is supposed to be the storage while a data view represent a query. Sorting and filtering are usually done at query level. Are you sure you want to duplicate the data?


    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP

    • Marked as answer by Cincy Steve Friday, May 25, 2012 1:59 PM
    Thursday, May 24, 2012 9:39 PM

All replies

  • A data set is supposed to be the storage while a data view represent a query. Sorting and filtering are usually done at query level. Are you sure you want to duplicate the data?


    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP

    • Marked as answer by Cincy Steve Friday, May 25, 2012 1:59 PM
    Thursday, May 24, 2012 9:39 PM
  • Sheng Jiang -

    Thanks for the reply.  I am working on my very first app, so I'm still learning how best to use .Net.  I will have to think more about the implications of your question. 

    As to why I want to do this in the first place, I see it as a judgement call about the implementation.  As one example, I have a UserControl that contains a ListBox that I need to populate with a subset of the data from one of my DataSet DataTables.  In order to maintain maximum separation between the UIL and DAL, the UI calls a data model object that returns the filtered DataTable.  Subsequently, the UI may need to Find a row in the filtered DataTable, which is why I want a subset DataTable (with the PrimaryKey set) instead of a DataView.  I know I could have the data model object return the entire DataTable (or I could simply expose the entire DataSet everywhere in my app) and then have the UI do the filtering via a BindingSource or a local DataView, but that requires the UI to know about the filtering criteria/syntax, which I'm trying to keep encapsulated/isolated in the DAL.  I realize that I am duplicating the data, but my data volume is very small and the encapsulation/isolation seems to help make the majority of the code simpler and easier to maintain.

    In any case, since posting the question, I've continued to experiment and have found what seems to be a viable 3 step solution (at least for my low data volume situation).   Here's the code:

    MyDataSet.MyDataRow[] drArray = 
      (MyDataSet.MyDataRow[]) myDataTable.Select(filterString, sortString, DataViewRowState.OriginalRows);
    MyDataSet.MyDataTable dt = (MyDataSet.MyDataTable) myDataTable.Clone();
    if (dr.Length != 0) foreach (MyDataSet.MyDataRow dr in drArray) dt.ImportRow(dr);

    I'd appreciate any additional observations (from anyone).  In any case, thanks for the help.

    Steve

    Friday, May 25, 2012 1:58 PM
  • Hi Steve,

    Seems like the code you provide should be correct for this issue, and what’s your requirement now?

    Best Regards,


    Tony Xiao [MSFT]
    MSDN Community Support | Feedback to us

    Monday, May 28, 2012 11:10 AM
  • Tony -

    That's it.  I have no additional requirement.  Thanks for the observation about the solution.

    Steve

    Monday, May 28, 2012 12:21 PM