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
  1. Home
  2. Web Development
  3. ASP.NET
  4. how to select first several datarow from a dataset, and how to create a dataset from datarow?

how to select first several datarow from a dataset, and how to create a dataset from datarow?

Scheduled Pinned Locked Moved ASP.NET
algorithmstutorialquestion
4 Posts 2 Posters 0 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.
  • S Offline
    S Offline
    Seraph_summer
    wrote on last edited by
    #1

    I need to implement the following: I have datatable, from which a dataset is created by selecting all rows. after sorting the dataset based on one column, then I want to get first 5 rows and create a new dataset with the top 5 rows, how to do? I have tried several methods, not successful. thanks for any hint.

    A 1 Reply Last reply
    0
    • S Seraph_summer

      I need to implement the following: I have datatable, from which a dataset is created by selecting all rows. after sorting the dataset based on one column, then I want to get first 5 rows and create a new dataset with the top 5 rows, how to do? I have tried several methods, not successful. thanks for any hint.

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      First of all, let me clear your concept about DataSet, DataTable and DataRow. 1. DataSet ( It is a collection of DataTables) 2. DataTable (It is a collection of DataRow) 3. DataRow (It is a collection of Data Elements) Well, Now if you have a datatable, you cant create a DataSet from it... as basically it is already a part of a dataset. If you are talking about coping some rows from one DataTable to a new DataTable, this can be done. Just I am going to create an example for you below :

      DataTable dtNew = dt.Clone();
      var dataSelect = (from r in dt.AsEnumerable()
      orderby r[0]
      select r).Take(5);
      foreach (DataRow dr in dataSelect)
      {
      DataRow newRow = dtNew.NewRow();
      for(int i=0; i

      After the foreach loop the DAtaTable dtNew will hold only the 5 elements selected in dataSelect.
      Hope you got the point. Just mold this into your requirement.

      Cheers :cool:

      Abhishek Sur


      My Latest Articles
      **Create CLR objects in SQL Server 2005
      C# Uncommon Keywords
      Read/Write Excel using OleDB

      **Don't forget to click "Good Answer" if you like to.

      S 1 Reply Last reply
      0
      • A Abhishek Sur

        First of all, let me clear your concept about DataSet, DataTable and DataRow. 1. DataSet ( It is a collection of DataTables) 2. DataTable (It is a collection of DataRow) 3. DataRow (It is a collection of Data Elements) Well, Now if you have a datatable, you cant create a DataSet from it... as basically it is already a part of a dataset. If you are talking about coping some rows from one DataTable to a new DataTable, this can be done. Just I am going to create an example for you below :

        DataTable dtNew = dt.Clone();
        var dataSelect = (from r in dt.AsEnumerable()
        orderby r[0]
        select r).Take(5);
        foreach (DataRow dr in dataSelect)
        {
        DataRow newRow = dtNew.NewRow();
        for(int i=0; i

        After the foreach loop the DAtaTable dtNew will hold only the 5 elements selected in dataSelect.
        Hope you got the point. Just mold this into your requirement.

        Cheers :cool:

        Abhishek Sur


        My Latest Articles
        **Create CLR objects in SQL Server 2005
        C# Uncommon Keywords
        Read/Write Excel using OleDB

        **Don't forget to click "Good Answer" if you like to.

        S Offline
        S Offline
        Seraph_summer
        wrote on last edited by
        #3

        thanks, but .take can be used for ADO.net?

        A 1 Reply Last reply
        0
        • S Seraph_summer

          thanks, but .take can be used for ADO.net?

          A Offline
          A Offline
          Abhishek Sur
          wrote on last edited by
          #4

          yes.. Actually Take comes as Extension Function to all Enumerable objects. You can use them easily. Only thing is to add System.Linq namespace to your project. The Static class Enumerable defines all those Extension Methods to IEnumerable objects. :thumbsup::thumbsup: Cheers..

          Abhishek Sur


          My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB

          **Don't forget to click "Good Answer" if you like to.

          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