Jayesh Acharya
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 CreateCachedPagedDataTable s(DataTabl e MasterDataTable, PagedDataSource PagedDataInformation)
{
DataSet dataSet = new DataSet();
string DTName = "";
int z = 0;
for (int i = 0; i < PagedDataInformation.PageC ount; 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.PageS ize; 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;
}
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 CreateCachedPagedDataTable
{
DataSet dataSet = new DataSet();
string DTName = "";
int z = 0;
for (int i = 0; i < PagedDataInformation.PageC
{
//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.PageS
{
//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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
great easy to follow completes what i need
ASKER
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 ...
ASKER
I have written the following function which does do what i am looking for thanks for the help
private DataSet CreateCachedPagedDataTable
(DataTable MasterDataTable
,PagedDataSource PagedDataInformation)
{
DataSet dataSet = new DataSet();
string DTName = "";
for (int i = 0; i < PagedDataInformation.PageC
{
//create a datatable
DTName = "Page" + i.ToString();
DataTable dt = new DataTable(DTName);
int RowStart = i * (PagedDataInformation.Page
int RowEnd = RowStart + PagedDataInformation.PageS
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;
}