Reputation: 2176
I'm reading some xml-files into a dataset, sort the datatable and generate for each row (if not exist) a new datatable in another dataset.
foreach (FileInfo f in files)
{
if (f.Name.StartsWith("ID_" + lbl_ID.Text))
{
dsDatei.Reset();
dsDatei.ReadXml(Application.StartupPath + f.Name);
DataTable dtSort = dsDatei.Tables[0].Clone();
dtSort.Columns[1].DataType = typeof(Int32);
foreach (DataRow dr in dsDatei.Tables[0].Rows)
{
dtSort.ImportRow(dr);
}
dtSort.DefaultView.Sort = "Number";
dsDatei.Tables.RemoveAt(0);
dsDatei.Tables.Add(dtSort.DefaultView.ToTable());
...some code
foreach (DataRow dr in dsDatei.Tables[0].Rows)
{
if (!dsAusdruck.Tables.Contains(dr[1].ToString()))
{
dsAusdruck.Tables.Add(dr[1].ToString());
dsAusdruck.Tables[dr[1].ToString()].Columns.Add("Position");
...add more columns
}
...some code
}
}
}
My problem is that for example in the first xml file the number 144 is missing, in the second xml file the number 144 exist and the datatable with the name 144 is created at the end of the dataset.
Later I create a pdf-file with the dataset, for each datatable a new page. So I get the number 144 at the last page instead between the pages with the numbers 143 and 145.
So how is it possible to sort the datatables in the dataset?
Thanks
Result for answer PinBack
Upvotes: 0
Views: 1145
Reputation: 2554
Order the tables with Linq:
UPDATE (reorder the Tablecollection):
DataSet loDs = new DataSet();
loDs.Tables.Add(new DataTable("141"));
loDs.Tables.Add(new DataTable("142"));
loDs.Tables.Add(new DataTable("143"));
loDs.Tables.Add(new DataTable("145"));
loDs.Tables.Add(new DataTable("144"));
var loSortedDataTableList = loDs.Tables.Cast<DataTable>().OrderBy(item => int.Parse(item.TableName)).ToList();
loDs.Tables.Clear();
foreach (DataTable loDt in loSortedDataTableList)
{
loDs.Tables.Add(loDt);
Console.WriteLine(loDt.TableName);
}
Output is:
141
142
143
144
145
Upvotes: 2