Solved

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

Posted on 2010-11-11
5
468 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
great easy to follow completes what i need
0
 
LVL 2

Author Comment

by:jhacharya
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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

10 Experts available now in Live!

Get 1:1 Help Now