Home > Net >  Data Table how to realize the merger of Table in the same field
Data Table how to realize the merger of Table in the same field

Time:10-08

I have a Data table as shown below:



I now need to ID the same column a merger, consolidation effect is as follows:



Excuse me, how to implement in c #?


CodePudding user response:

https://blog.csdn.net/hhongjia/article/details/95208459

CodePudding user response:

reference 1st floor ManBOyyy response:
https://blog.csdn.net/hhongjia/article/details/95208459

Hello, I need to do the same table to merge, the need to do?

CodePudding user response:

refer to the second floor qq_18191763 response:
Quote: refer to 1st floor ManBOyyy response:

https://blog.csdn.net/hhongjia/article/details/95208459

Hello, I need to do the same table to merge, the need to do?

All the same, have a good think about it

CodePudding user response:

If it is SQL language can you?
Actually the same, use the DataTable GroupBy grouping, and a simple processing logic in the can

CodePudding user response:

wrong, cannot use the GroupBy,
(1) create a fit your combined DataTable,
(2) cycle you original DataTable, then insert into the new DataTable
But there is a problem, are you sure you is the result of the combined DataTable? Because the same item you don't know how many, many of one thousand items are the same, your DataTable fields to dynamic change,
Conducive to List The way is not better

CodePudding user response:

Should be able to realize the function you want to, but the efficiency is not high, have any good ideas together discuss


//for ID grouping DataTable: [dt_ID]
Select the ID from YouTable group by ID

Dt_ID. PrimaryKey=new DataColumn [] {dt_ID. Columns (" ID ")};



for (int i=0; I & lt; Dt_ID. Rows. Count; I++)
{

//create a temporary table [dt_Temp]
The DataTable dt_Temp=new DataTable ()
Dt_Temp. Columns. The Add (" ID ", typeof (int));

String fieldValues=dt_ID. ID;


//get the same ID recordset: (dt)
Select Training, Duration from YouTable where ID=dt_ID. ID

For (int j=0; J & lt; Dt. Rows. Count; J++)
{
String fieldName1="Training" + j.
String fieldName2="Duration" + j
Dt_Temp. Columns. The Add (fieldName1, typeof (string));
Dt_Temp. Columns. The Add (fieldName2, typeof (string));

FieldValues +=", "+ dtBind. Rows [j] [r]." Training "the ToString () +", "+ dtBind. Rows [j] [r]." Duration "ToString ()
}

//add the line, the data fill
String [] str_Array=fieldValue. Split (', ');
Dt_Temp. Rows. The Add (str_Array);


Dt_ID. Merge (dt_Temp, false, MissingSchemaAction. Add);//merge to entity list, the same field with
}

CodePudding user response:

Sorry, forgot to say, it is a way of thinking, is not a complete code, need to perfect the

CodePudding user response:

reference 5 floor HyoJung reply:
wrong, cannot use the GroupBy,
(1) create a fit your combined DataTable,
(2) cycle you original DataTable, then insert into the new DataTable
But there is a problem, are you sure you is the result of the combined DataTable? Because the same item you don't know how many, many of one thousand items are the same, your DataTable fields to dynamic change,
Conducive to List The way isn't it better to


List How to realize ah, please guide, thank you.

CodePudding user response:

Or you can use SQL to search the transfer list

Either the DataTable in the back end you use to construct a new DataTable,

CodePudding user response:

refer to the eighth floor [big chu
reply:]
Quote: refer to the fifth floor HyoJung reply:

wrong, cannot use the GroupBy,
(1) create a fit your combined DataTable,
(2) cycle you original DataTable, then insert into the new DataTable
But there is a problem, are you sure you is the result of the combined DataTable? Because the same item you don't know how many, many of one thousand items are the same, your DataTable fields to dynamic change,
Conducive to List The way isn't it better to


List How to realize ah, please guide, thank you,

If you figure out what is interface on this show, or the data source must be in the format of the data? If the screen display nested entity class is better than the DataTable operation

CodePudding user response:

To create the first column, and then you create a good the datatable with dt. NewRows function, can,


DataTable this object has a special processing method of columns and rows,

If really don't know, call me, I'll give you write a demo
  •  Tags:  
  • C#
  • Related