Wednesday, May 16, 2012

how to merge rows in datatable which had same value (VB)

I have one datatable tempDT with value :



Serial_No                      testong
---------------------------------------
DTSHCSN001205035919201 [ OUT ] <br/> Partner : 90000032 <br/> Date : 16 Feb 2012
DTSHCSN001205035919201 [ IN ] <br/> Partner : 90000032 <br/> Date : 16 Feb 2012
DTSHCSN001205035919201 [ OUT ] <br/> Partner : 80000869 <br/> Date : 31 Mar 2012
DTSHCSN001205035919201 [ IN ] <br/> Partner : 80000869 <br/> Date : 31 Mar 2012


The problem is I want merge duplicate serial_no into one row which the value of testong adding to new column.



I have tried many ways, but I can't find the solution.



Here is my code behind :



    Dim tempDt = GetItemDataTable()
Dim dtData As New DataTable

dtData.Columns.Add("Serial_No")
Dim i As Integer = 0
Dim row As DataRow

For Each row In tempDt.Rows
i += 1
Dim dr As DataRow = dtData.NewRow
dr("Serial_No") = row(0)
If dr("Serial_No") = row(0) Then
Dim colBaru As GridViewDataTextColumn = New GridViewDataTextColumn()
colBaru.Caption = i
colBaru.FieldName = i
colBaru.CellStyle.HorizontalAlign = HorizontalAlign.Center
colBaru.HeaderStyle.HorizontalAlign = HorizontalAlign.Center
colBaru.Width = 150

colBaru.UnboundType = DevExpress.Data.UnboundColumnType.Integer
colBaru.VisibleIndex = grid.VisibleColumns.Count
colBaru.PropertiesTextEdit.DisplayFormatString = "d1"
colBaru.PropertiesTextEdit.EncodeHtml = True
grid.Columns.Add(colBaru)
dtData.Columns.Add(i)
dr(i) = row(3)
dtData.Rows.Add(dr)

Else
dr("Serial_No") = row(0)
dtData.Rows.Add(dr)
End If


When I debug the result is :
wrong result



But I wanted the result is like this :
enter image description here





No comments:

Post a Comment