Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Merging Filtered Data

Merging Filtered Data

Scheduled Pinned Locked Moved ASP.NET
businesshelpquestion
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    asifmaniar
    wrote on last edited by
    #1

    I have to filter data from two different but identical datatables into two different views. Then i have to merge the two views. Any ideas how can I do that. If I copy data from one view into another its no longer visible as it doesnt not necessarily meet the filter requirements for that view. I think i need to copy them to a 3rd datatable. Any help would be appreciated.

    M 1 Reply Last reply
    0
    • A asifmaniar

      I have to filter data from two different but identical datatables into two different views. Then i have to merge the two views. Any ideas how can I do that. If I copy data from one view into another its no longer visible as it doesnt not necessarily meet the filter requirements for that view. I think i need to copy them to a 3rd datatable. Any help would be appreciated.

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, IMO, there's no need to use a third table here. The DataSet class has the Merge method which you can use to merge data from two tables and you can do the filtering later. The sample code below is used to get all customers and suppliers whose names start with "A" from the Northwind database:

      private DataView GetDataSource()
      {
      //Get data from the first table.
      SqlConnection con1 = new SqlConnection("server=localhost;uid=username;pwd=password;database=NorthWind;Integrated security=false");
      SqlDataAdapter adapter1 = new SqlDataAdapter("select CompanyName as ColName from Customers", con1);
      DataSet ds1 = new DataSet();
      adapter1.Fill(ds1, "Results");

      //Get data from the second table
      //If the column name is the same, you don't need to use the alias.
      SqlDataAdapter adapter2 = new SqlDataAdapter("select CompanyName as ColName from Suppliers", con1);
      DataSet ds2 = new DataSet();
      adapter2.Fill(ds2, "Results");
              
          //Merge data.
      ds1.Merge(ds2);
                 
          //Filter data.
      //You can filter data before merging the two tables.
      DataView view = ds1.Tables\[0\].DefaultView;
      view.RowFilter = "ColName like 'A\*'";
                
      return view;
      

      }

      For more information, see: Merging DataSet Contents[^] DataSet.Merge Method[^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups