Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now