Link to home
Create AccountLog in
Avatar of Jayesh Acharya
Jayesh AcharyaFlag for United States of America

asked on

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

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;
        }

ASKER CERTIFIED SOLUTION
Avatar of jagssidurala
jagssidurala
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Avatar of Jayesh Acharya

ASKER

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;
        }
great easy to follow completes what i need
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 ...