I get a DataTable
like below (first image),in the last column I have concatenate item(separate by ";") that I want to disassemble like the second image ,I have tried sevral ways that does not work ,if any one can help me . Ps :I dont know the numbers of element in any cell of the last columns (the belows are just exemples):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
DataTable dt1 = new DataTable();
dt1.Columns.Add("produit", typeof(string));
dt1.Columns.Add("test1", typeof(int));
dt1.Columns.Add("test3", typeof(int));
dt1.Columns.Add("logistique", typeof(string));
dt1.Columns.Add("amount", typeof(string));
dt1.Columns.Add("testAjout", typeof(string));
dt1.Columns.Add("testAjout1", typeof(string));
dt1.Rows.Add(new object[] { "P1", 1, 3, "t3;t1;t2", "t4;t5;t6;tr", "T34;T25", "Rahima;T33;T55;T66" });
dt1.Rows.Add(new object[] { "P2", 1, 4, "t9;t10;t55", "t2;t1;t3", "t34;t45", "Nadji" });
DataTable dt2 = dt1.Clone();
foreach (DataRow row in dt1.AsEnumerable())
{
string produit = row.Field<string>("produit");
int test1 = row.Field<int>("test1");
int test3 = row.Field<int>("test3");
//int test4 = row.Field<int>("test4");
string[] splitLogistique = row.Field<string>("logistique").Split(new char[] { ';' }).ToArray();
string[] splitAmount = row.Field<string>("amount").Split(new char[] { ';' }).ToArray();
string[] splitAjout = row.Field<string>("testAjout").Split(new char[] { ';' }).ToArray();
string[] splitAjout1 = row.Field<string>("testAjout1").Split(new char[] { ';' }).ToArray();
int[] Lengths = new int[4];
Lengths[0] = splitAjout.Length;
Lengths[1] = splitAjout1.Length;
Lengths[2] = splitAmount.Length;
Lengths[3] = splitLogistique.Length;
Array.Sort(Lengths);
Dictionary<string, int> dic = new Dictionary<string, int>();
dic.Add("splitAjout", splitAjout.Length);
dic.Add("splitAjout1", splitAjout1.Length);
dic.Add("splitAmount", splitAmount.Length);
dic.Add("splitLogistique", splitLogistique.Length);
int maxLength = Math.Max(Math.Max(splitLogistique.Length, Math.Max(splitAmount.Length, splitAjout.Length)), splitAjout1.Length);
for (int i = 0; i < maxLength; i++)
{
if ((i < splitLogistique.Length) && (i < splitAmount.Length) && (i < splitAjout.Length) && (i < splitAjout1.Length))
{
dt2.Rows.Add(new object[] { produit, test1, test3, splitLogistique[i], splitAmount[i], splitAjout[i], splitAjout1[i] });
}
if ((i >= splitLogistique.Length && (i < splitAmount.Length) && (i < splitAjout.Length) && (i < splitAjout1.Length))
|| (i >= splitLogistique.Length && (i < splitAjout1.Length))
|| (i >= splitLogistique.Length && (i < splitAjout.Length))
|| (i >= splitLogistique.Length && (i < splitAmount.Length))
|| (i >= splitLogistique.Length && (i < splitAjout.Length) && (i < splitAjout1.Length))
|| (i >= splitLogistique.Length && (i < splitAmount.Length) && (i < splitAjout.Length))
|| (i >= splitLogistique.Length && (i < splitAmount.Length) && (i < splitAjout1.Length))
|| (i >= splitLogistique.Length))
{
//DataRow newRow = dt2.Rows.Add();
//newRow.ItemArray = new object[] { produit, test1, test3 , splitAmount[i] , splitAjout[i] };
dt2.Rows.Add(new object[] { produit, test1, test3, splitAmount[i], splitAjout[i], splitAjout1[i] });
}
if ((i >= splitAmount.Length && (i < splitLogistique.Length) && (i < splitAjout.Length) && (i < splitAjout1.Length))
|| (i >= splitAmount.Length && (i < splitAjout1.Length))
|| (i >= splitAmount.Length && (i < splitAjout.Length))
|| (i >= splitAmount.Length && (i < splitLogistique.Length))
|| (i >= splitAmount.Length && (i < splitAjout.Length) && (i < splitAjout1.Length))
|| (i >= splitAmount.Length && (i < splitLogistique.Length) && (i < splitAjout.Length))
|| (i >= splitAmount.Length && (i < splitLogistique.Length) && (i < splitAjout1.Length))
|| (i >= splitAmount.Length))
{
//DataRow newRow = dt2.Rows.Add();
dt2.Rows.Add(new object[] { produit, test1, test3, splitAjout[i], splitLogistique[i], splitAjout1[i] });
}
if (i >= splitAjout1.Length && (i < splitAmount.Length) && (i < splitLogistique.Length) && (i < splitAjout.Length))
{ //DataRow newRow = dt2.Rows.Add();
dt2.Rows.Add(new object[] { produit, test1, test3, splitAmount[i], splitLogistique[i], splitAjout[i] });
}
if ((i >= splitAjout.Length && (i < splitAmount.Length) && (i < splitLogistique.Length) && (i < splitAjout.Length))
|| (i >= splitAjout.Length && (i < splitAjout1.Length))
|| (i >= splitAjout.Length && (i < splitLogistique.Length))
|| (i >= splitAjout.Length && (i < splitAmount.Length))
|| (i >= splitAjout.Length && (i < splitLogistique.Length) && (i < splitAjout1.Length))
|| (i >= splitAjout.Length && (i < splitAmount.Length) && (i < splitLogistique.Length))
|| (i >= splitAjout.Length && (i < splitAmount.Length) && (i < splitAjout1.Length))
|| (i >= splitAjout.Length))
{
//DataRow newRow = dt2.Rows.Add();
dt2.Rows.Add(new object[] { produit, test1, test3, splitAmount[i], splitLogistique[i] });
}
}
}
DataRow[] currentRows = dt2.Select(null, null, DataViewRowState.CurrentRows);
foreach (DataRow row in currentRows)
{
foreach (DataColumn column in dt2.Columns)
Console.Write("\t{0}", row[column]);
Console.WriteLine("\t" + row);
}
}
}
}
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication9
{
class Program
{
const int HEADER_COLS = 3;
static void Main(string[] args)
{
DataTable dt1 = new DataTable();
dt1.Columns.Add("produit", typeof(string));
dt1.Columns.Add("test1", typeof(int));
dt1.Columns.Add("test3", typeof(int));
dt1.Columns.Add("logistique", typeof(string));
dt1.Columns.Add("amount", typeof(string));
dt1.Columns.Add("testAjout", typeof(string));
dt1.Columns.Add("testAjout1", typeof(string));
dt1.Rows.Add(new object[] { "P1", 1, 3, "t3;t1;t2", "t4;t5;t6;tr", "T34;T25", "Rahima;T33;T55;T66" });
dt1.Rows.Add(new object[] { "P2", 1, 4, "t9;t10;t55", "t2;t1;t3", "t34;t45", "Nadji" });
DataTable dt2 = dt1.Clone();
int cols = dt1.Columns.Count;
foreach (DataRow row in dt1.AsEnumerable())
{
string produit = row.Field<string>("produit");
int test1 = row.Field<int>("test1");
int test3 = row.Field<int>("test3");
//[column][number items spit by semicolon]
string[][] splitData = row.ItemArray.Skip(HEADER_COLS).Select(x => ((string)x).Split(new char[] { ';'}).ToArray()).ToArray();
int maxLength = splitData.Select(x => x.Length).Max();
//add rows to data table using column with most semicolons
for (int i = 0; i < maxLength; i++)
{
DataRow newRow = dt2.Rows.Add();
newRow.ItemArray = new object[] { produit, test1, test3 };
for(int col = HEADER_COLS; col < cols; col++)
{
if ( i < splitData[col - HEADER_COLS].Length)
{
newRow[col] = splitData[col - HEADER_COLS][i];
}
}
}
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.