none
Does filtering and sorting a dataview affect the underlying datatable? RRS feed

  • Question

  • I need to create a datatable from a sub set of another table and sort in different order and i was wondering if my doing so would affect the underlying table?  I need that to be in the same order as it was before sorting the dataview.

     

    myDt= process(getData);

    Dataview myDV= myDt.DefualtView;

    myDv.Rowfilter ="Age >52";

    myDv.sort = "ShoeSize Desc";

    myNewDt= myDv.ToTable();

    Also,,, Could i just get 3 columns from the orig table?  like Age, ShoeSize, Likesbowling ?

    Wednesday, May 5, 2010 2:58 PM

Answers

  • Calling DataView.ToTable() does not affect the original DataTable.

    Also, you can pass in the column names you want into two of the overloaded ToTable methods, like so:

    DataTable myNewDt = myDv.ToTable("NewTableName", false, "Age", "ShoeSize", "LikesBowling");

    The first argument sets the TableName property of the DataTable.  The second argument controls whether or not distinct sets of rows are generated.  The remaining arguments are the names of the columns to include.  Specifying true for the second argument, in this case, means that if there are multiple rows with the same values for Age, ShoeSize and LikesBowling, that the repeats will be omitted from the new DataTable.  This approach is a little easier than copying over all of the columns and then removing the ones you don't want. 

    I hope this information proves helpful.


    David Sceppa
    Thursday, May 6, 2010 9:00 PM
    Moderator
  • 1.  If you want a DataView totally independent from the table, you can construct your own "DataView myDV = new DataView(myDt);" instead of using the DefaultView.

    2.  You can use myNewDt.Columns.Remove(...) to take out the columns you do not want.  Typically, DataTable objects should not contain a lot of data, so this probably will not matter that you copied this data only to remove it right after.

     

    • Proposed as answer by Gopi V Thursday, May 6, 2010 5:46 AM
    • Marked as answer by Alex LiangModerator Wednesday, May 12, 2010 1:16 PM
    Wednesday, May 5, 2010 10:25 PM

All replies

  • 1.  If you want a DataView totally independent from the table, you can construct your own "DataView myDV = new DataView(myDt);" instead of using the DefaultView.

    2.  You can use myNewDt.Columns.Remove(...) to take out the columns you do not want.  Typically, DataTable objects should not contain a lot of data, so this probably will not matter that you copied this data only to remove it right after.

     

    • Proposed as answer by Gopi V Thursday, May 6, 2010 5:46 AM
    • Marked as answer by Alex LiangModerator Wednesday, May 12, 2010 1:16 PM
    Wednesday, May 5, 2010 10:25 PM
  • Hi,

    To create a new DataTable using the Existing Table use like this 

    DataTable myDt = process(getData);
    // Extract the Data Rows from the DataTable
    Dataview myDV= new DataView(myDt, "Age >52", "ShoeSize Asc");
    myNewDt= myDv.Table.Clone();
    // Remove the Columns from the Table.
    myNewDt.Columns.Remove("ColumnName1");
    myNewDt.Columns.Remove("ColumnName2");
    myNewDt.Columns.Remove("ColumnName3");

    Hope this would helps you a bit.

    Note : Try using the LINQ on this.... http://msdn.microsoft.com/en-us/vbasic/bb688085.aspx


    Best Regards,
    Gopi V

    If you have found this post helpful, please click the Vote as Helpful link (the green triangle and number on the top-left).

    If this post answers your question, click the Mark As Answered link below. It helps others who experience the same issue in future to find the solution.

    Thursday, May 6, 2010 5:51 AM
  • Calling DataView.ToTable() does not affect the original DataTable.

    Also, you can pass in the column names you want into two of the overloaded ToTable methods, like so:

    DataTable myNewDt = myDv.ToTable("NewTableName", false, "Age", "ShoeSize", "LikesBowling");

    The first argument sets the TableName property of the DataTable.  The second argument controls whether or not distinct sets of rows are generated.  The remaining arguments are the names of the columns to include.  Specifying true for the second argument, in this case, means that if there are multiple rows with the same values for Age, ShoeSize and LikesBowling, that the repeats will be omitted from the new DataTable.  This approach is a little easier than copying over all of the columns and then removing the ones you don't want. 

    I hope this information proves helpful.


    David Sceppa
    Thursday, May 6, 2010 9:00 PM
    Moderator