0

how to split datatable into multiple datatable based on row count in C# .Please give the suggestion to me .

MySqlDataAdapter da = new MySqlDataAdapter("", con);
                    da.SelectCommand.Parameters.AddWithValue("@sid", specializationId);
                    da.SelectCommand.Parameters.AddWithValue("@uid", userId);
                    da.SelectCommand.Parameters.AddWithValue("@counter", counter);
                    da.Fill(dt);
Girish Patil
  • 19
  • 1
  • 6
  • What do you actually want to do? Paging? – TriV Apr 24 '17 at 09:53
  • 1. Either format or remove that query as no one will be able to read it as 1 huge line. 2. Ask a question instead of just a title and code dump – EpicKip Apr 24 '17 at 09:53

1 Answers1

1

You know how to use database paging? Then what is the problem? Just fill a DataTable, store this DataTable in a List<DataTable> or DataSet and then select the next row-set into another DataTable and add that to the collection.

If you have filled already a big DataTable with all rows and you want to split it into multiple DataTables, this code does it:

int tableSize = 100;               // for example
DataSet allTables = new DataSet(); // or List<DataTable>

for (int i = 0; i < bigTable.Rows.Count; i += tableSize)
{
    DataTable tbl = bigTable.Clone(); // same columns, empty
    for (int ii = 0; ii < tableSize; ii++)
    {
        if (i + ii == bigTable.Rows.Count) break;
        tbl.ImportRow(bigTable.Rows[i + ii]);
    }

    allTables.Tables.Add(tbl);
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I want each table of 20 rows. If the datatable row count is 100 then it splits into 5 tables – Girish Patil Apr 24 '17 at 10:22
  • @GirishPatil: well, then change `tableSize = 100` to `tableSize = 20` – Tim Schmelter Apr 24 '17 at 10:46
  • can you please look into one of my [question](https://stackoverflow.com/questions/55688229/how-to-split-data-table-into-multiple-tables-with-adding-1-minute-delay) related to it? – Moeez Apr 16 '19 at 06:46