Solved

how to select the next 10 rows of a datatable c#

Posted on 2010-11-11
5
475 Views
Last Modified: 2012-05-10
I have a Master DataTable that does not have a primary key,

I want to be able to get rows 0 to 9 of the Master and put them in datatable 1
rows 10 to 19 into datatable 2
rows 20 to 26 into datatable 3

Here is the function i have designed so far but now I am stuck on trying to find out how to loop through the rows ...

       private DataSet CreateCachedPagedDataTables(DataTable MasterDataTable, PagedDataSource PagedDataInformation)
        {
            DataSet dataSet = new DataSet();
            string DTName = "";
            int z = 0;

            for (int i = 0; i < PagedDataInformation.PageCount; i++)
            {
                //create a datatable
                DTName = "Page" + i.ToString();
                DataTable dt = new DataTable(DTName);

                //loop through the master data table by pagecount
                //check if the row is there
                for (int j = 0; j < PagedDataInformation.PageSize; j++)
                {

                    //add each row of MasterDataTable
                    //to the datatable just created

                    //how to add the row??

                   
                    //how to reset the starting postion ofthe row
                    z++;
                }

                //add this datatable to the DataSet
                dataSet.Tables.Add(DTName);
            }


            return dataSet;
        }

0
Comment
Question by:jhacharya
  • 3
5 Comments
 
LVL 12

Accepted Solution

by:
jagssidurala earned 275 total points
ID: 34114037
you need change function like below

private DataSet CreateCachedPagedDataTables(DataTable MasterDataTable, PagedDataSource PagedDataInformation)
        {
            DataSet dataSet = new DataSet();
            string DTName = "";
            int z = 0;
           
            for (int i = 0; i < PagedDataInformation.PageCount; i++)
            {
               
                DTName = "Page" + i.ToString();
                DataTable dt = new DataTable(DTName);

      int RowStart = i * (PagedDataInformation.PageSize + 1) ;
      int RowEnd = RowStart + PagedDataInformation.PageSize;
             
                for (int j = RowStart ; j <= RowEnd; j++)
                {
            DataRow myNewRow;
            myNewRow = dt.NewRow();
            myNewRow["customerId"] = MasterDataTable.Rows[j]["customerId"];
            myNewRow["username"] = MasterDataTable.Rows[j]["username"] ;
            .
                              .
            
            dt.Rows.Add(myNewRow)
                }

                dataSet.Tables.Add(dt);
            }


            return dataSet;
        }
0
 
LVL 15

Assisted Solution

by:x77
x77 earned 225 total points
ID: 34114474
ImportRow allows copy values from one row to a Table that has corresponding column names.
This simplifies the copy, you do´nt need create each row and copy each value.

               Dim Tdpos = Me.TdPos.Clone 'Solo las filas seleccionadas
               Tdpos.MinimumCapacity = Td.Rows.Count * 3
               Tdpos.BeginLoadData()
               For Each tr In Trabajos
                   For Each r In Me.TdPos.Lookup(tr.Codtra)
                       Tdpos.ImportRow(r)
                   Next
               Next
               Tdpos.EndLoadData()
0
 
LVL 2

Author Comment

by:jhacharya
ID: 34115260
thanks ...
I have written the following function which does do what i am looking for thanks for the help

        private DataSet CreateCachedPagedDataTables
            (DataTable MasterDataTable
            ,PagedDataSource PagedDataInformation)
        {
            DataSet dataSet = new DataSet();
            string DTName = "";


            for (int i = 0; i < PagedDataInformation.PageCount; i++)
            {
                //create a datatable
                DTName = "Page" + i.ToString();
                DataTable dt = new DataTable(DTName);



                int RowStart = i * (PagedDataInformation.PageSize ) ;
                int RowEnd = RowStart + PagedDataInformation.PageSize ;

                for (int j = RowStart; j < RowEnd; j++)
                {
                    DataRow myNewRow;
                    myNewRow = dt.NewRow();
                    try { myNewRow = MasterDataTable.Rows[j]; }  catch (Exception) { }
                    dt.ImportRow(myNewRow);

                }

                //add this datatable to the DataSet
                dataSet.Tables.Add(DTName);
            }


            return dataSet;
        }
0
 
LVL 2

Author Closing Comment

by:jhacharya
ID: 34115266
great easy to follow completes what i need
0
 
LVL 2

Author Comment

by:jhacharya
ID: 34116618
ok i seem to have an issue with this becasue, it copy over the rows into the dt datatable, it does not seem to copy over the column information ... not sure how to get around this ...
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Video Player 12 27
Problem!!! 8 37
C# winforms programmitically move panels 6 43
Change to file doesn't show up 16 51
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question