Solved

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

Posted on 2010-11-11
5
474 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help on C# Linq query on object 6 63
Error in script 11 58
SqlDataBase 7 48
VS2013 publish C# application Created folder -- where is it??? 10 18
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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