# dividir la tabla de datos

I have the below table datatable.

``````Name      classhours        Coursename
======================================
Tom            2              A
Jack           2              B
Joe            2              C
stella         3              D
Jose           3              E
``````

I need to merge the table rows into those having same classhours and sum the classhours.

Tabla b

``````Name                classhours       Coursename
===============================================
Tom,Jack,Joe            6              A,B,C
Stella,Jose             6               D,E
``````

Now i need to further split it.. in the course name column i can have max of 2 courses.. so i need to break the Table B into below table

``````Name                classhours       Coursename
===============================================
Tom,Jack                4               A,B
Joe                     2                C
Stella,Jose             6               D,E
``````

How do we further split it to check the count of CourseName not more than 2? Please let me know if you need further explanation. Check attachement for the code Please help.

Aqui esta el codigo

``````for (int i = 0; i < dataTable.Rows.Count; i++)
{
for (int j = i + 1; j < dataTable.Rows.Count; j++)
{
if(datatable.Rows[i]["classhours"].ToString() == datatable.Rows[j]["classhours"].ToString())
{
//Adding the second row and third row to the first row and deleting the second and third row.
}
}
}
``````

preguntado el 01 de febrero de 12 a las 22:02

## 1 Respuestas

Well, Looks like you just need a very small change.

``````for (int i = 0; i < dt.Rows.Count; i++)
{
bool notCombined = true;
int target = dt.Rows[i]["hours"];
for (int j = i + 1; j < dt.Rows.Count && notCombined; j++)
{
if(target == dt.Rows[j]["hours"].ToString())
{
dt.Rows[i]["name"] += "," + dt.Rows[j]["Name"];
dt.Rows[i]["hours"] += target;
dt.Rows[i]["course"] += "," + dt.Rows[j]["course];
dt.Rows[j].Delete();
notCombined = false;
}
}
``````

Since you're only combining two classes, you know that after you've found a match for dataTable[i] you can stop looking.

However, this only works if you can assume that each Course Name is unique (as in your example.) I'm going to assume that this is probably untrue, and if that's the case you have to do a bit deeper checking.

``````// Assuming classhours matches for dataTable.Rows[i] and dataTable.Rows[j]

if( dt.Rows[i]["course"].indexOf( dt.Rows[j]["course"] ) != -1 )
{ // dt already has the same course
dt.Rows[i]["Name"] += "," + dt.Rows[j]["Name"];
dt.Rows[i]["hours"] += target;
dt.Rows[j].Delete();
}
else if( dt.Rows[i]["course"].indexOf(',') == -1 )
{// dt[i] either contains just one course
dt.Rows[i]["name"] += "," + dt.Rows[j]["Name"];
dt.Rows[i]["course"] += "," + dt.Rows[j]["course];
dt.Rows[i]["hours"] += target;
dt.Rows[j].Delete();
}
``````

Respondido 02 Feb 12, 02:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.