Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-11-11
5
Medium Priority
?
484 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 12

Accepted Solution

by:
jagssidurala earned 1100 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 900 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

604 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