Click here to Skip to main content
15,896,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a normal application developed using C# winform, in which iam generating a report. The result of the query will be returned as single datatable which contains n number of columns.Now,
I want to split the output datatable into several datatables based on the column count.
For example if the output datatable contains 75 columns,then i want to split that into 5 datatables as 15 columns in each. Here column count as well as columns names are not fixed values.

Can you help me on this.


Thanks.
Posted
Comments
[no name] 15-Jun-14 7:50am    
Help you with what? Have you actually tried anything? What was the problem with what you have tried?

1 solution

Firstly I'm going to say that you will usually get quicker and better responses if you have a go at the problem yourself, and post code that you are having problems with.

There are also several examples via Google or in CodeProject of ways of splitting a datatable. However, to be fair, most of them seem to assume that you know the names of the columns you are trying to split on.

I've come up with this solution using this test data
const int noOfCols = 72;
DataTable dt = new DataTable();

//populate the datatable with some stuff
for (int i = 0; i < noOfCols; i++)
    dt.Columns.Add("ACol" + i.ToString(), typeof(string));

    for (int j = 0; j < 100; j++)
    {
        DataRow dr = dt.Rows.Add();
        for (int i = 0; i < noOfCols; i++)
            dr.SetField<string>(i, "Row" + j.ToString() + ":" + "Col" + i.ToString());
    }
First warning - all of my test data is of type string - you may need to change things around if you have different datatypes in your datatables.

2nd warning - note that I have chosen the number of columns NoOfCols = 72 which doesn't divide exactly by 15. You should see why in the code below.
I've set up a variable for the column number to split on.
C#
int splitCol = 15;  //change or calculate this as required. 15 columns per table
//so our tables will have columns 0-14, 15-29, 30-44, 45-59, 60-71

I also need somewhere to store my new datatables. As the .net DataSet conveniently already has a DataTable collection I'll use one of them
// Somewhere to keep all of our new datatables
DataSet ds = new DataSet();
DataTable comes with a handy property called DefaultView[^]
Quote:
Gets a customized view of the table that may include a filtered view, or a cursor position.
I'm using that in the code below to get the new datatable based on a list of column names that I temporarily store in an array.
C#
// starting point
int startCol = 0;
int endCol = splitCol - 1;

while (startCol < dt.Columns.Count)
{
    //This check is in case the number of columns isn't always consistent
    // e.g. 74 columns in original, split into datatables of 4 tables of 15
    // columns and 1 of 14 columns
    int newCols = (endCol < dt.Columns.Count) ? splitCol : dt.Columns.Count - startCol;

    // Set up an array of the column names we're going to use
    string[] ColNames = new string[newCols];
    //Get the column names from the original DataTable
    for (int i = 0; i < newCols; i++)
        ColNames[i] = dt.Columns[startCol + i].ColumnName;

    // Get just those columns from the original datatable into
    // a new datatable in our dataset
    ds.Tables.Add(dt.DefaultView.ToTable(false, ColNames));

    // Move on to the next table
    startCol += splitCol;
    endCol += splitCol;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900